Choosing elasticsearch index dynamically python - elasticsearch

How can I choose an index dynamically using python. My current code is as follows:
import os
import time
import datetime
from datetime import datetime
import shutil
from elasticsearch import Elasticsearch
from esindex import createIndex
client=Elasticsearch()
I have 1000's of images and the name of the image would be: 1559624525_cb704087042c76bf.jpg. I am splitting the name into two parts, timestamp (1559624525) and machid (cb704087042c76bf) and writing it into an es index.
path="/home/ubuntu/images/"
for root, dirs, files in os.walk(path):
for name in files:
try:
dat=name.split('_')[0]
machid=name.split('_')[1]
machid=machid.split('.')[0]
createIndex(machid) ##this creates index dynamically
dat=int(dat)
dat=datetime.utcfromtimestamp(dat).strftime('%d-%m-%Y %H:%M:%S')
dte=dat.split(' ')[0]
id=id+1
doc = {'sequence_id':id,'imagename': name, 'time_stamp': dat, 'date'=dte}
#print(doc)
client.index(index=machid, body=doc) ##this line is not working. If i comment this out, shutil works.
shutil.copy('/home/ubuntu/images/'+ name, '/home/ubuntu/test/')
os.remove('/home/ubuntu/images/' + name)
except Exception:
continue
how can i input a document into elasticsearch by dynamically choosing the index.

print the Exception in except block
Check machid is not empty
Since there is not any errors use Cat APIS to check any indeces created in your ES
curl -X GET "localhost:9200/_cat/indices"

Apparently I figured that my code works. In createIndex function I had a date field, which was set to date, however the above code parses the date as text. To test, when I changed the date to text in createIndex, everything fell in place. Replying this to complete the QA loop.

Related

Read contents of CSV file into list and print contents of list into web text field using pyautogui

When I create a list from values in a CSV file, I am able to print out the contents of the list using the built in print function, but when I try to enter the elements of the list into an input box using pyautogui,only the first element of the list is printed.
Here is the code which is not working with screenshot of error attached.
Idle Error Message
import csv, pyautogui, time
file = open('serials.csv', 'r')
serial_list = list(csv.reader(file))
file.close
print(serial_list)
time.sleep(5)
i = 0
for serial in serial_list:
pyautogui.typewrite(serial_list[i][i])
i +=1
I was able to get the desired results using the Python code below which I generated using VBA, but would like to learn how to do this properly using Python.
import pyautogui, time
time.sleep(3)
pyautogui.write("2300-xxxx1")
pyautogui.press('tab')
pyautogui.write("2300-xxxx2")
pyautogui.press('tab')
pyautogui.write("2300-xxxx3")
pyautogui.press('tab')
pyautogui.write("2300-xxxx4")
pyautogui.press('tab')
pyautogui.write("2300-xxxx5")
yo, I think issue is in this line of code:
pyautogui.typewrite(serial_list[i][i])
Here, you trying to access a specific element of the 2D list serial_list using serial_list[i][i] where i is the index variable. However, this will only give you the first element of the list.
You should use the variable serial that you defined in the for loop to access the current element of the list:
pyautogui.typewrite(serial)
Also, since the serial_list is a 2D list of strings, you want to convert the elements of list to strings before passing it to the pyautogui.typewrite() function like this:
for serial in serial_list:
pyautogui.typewrite(str(serial))
Also, you should call the file.close method with paranthesis file.close() to close the file properly.
Also, you should use the pyautogui.press('tab') function after each pyautogui.typewrite() function call to move to the next input box.
So the final code will look like:
import csv, pyautogui, time
file = open('serials.csv', 'r')
serial_list = list(csv.reader(file))
file.close()
print(serial_list)
time.sleep(5)
for serial in serial_list:
pyautogui.typewrite(str(serial))
pyautogui.press('tab')
Now it will work.
why? cuz 🅱️

Is it possible to replace one directive with another one

I would like to create a substitution (or similar) that transforms one directive into another.
For example:
In our sphinx based documentation, we use Admonitions to create certain note and warning boxes.
However, if we use
.. note:: This is a Note
The title of the box is Note, and This is a Note becomes the first paragraph.
In contrast, this directive
.. admonition:: This is a Note
:class: note
produces a note box with the desired title.
To make it easier for other editors, I would like to create a substitution, that replaces the first with the second.
Is there anything this can be done with in sphinx?
Yes, it can be done. You have to add a custom directive to Sphinx. Create a Python module (like mydirectives.py next to conf.py) with the following:
import os
import os.path
import re
import subprocess
import docutils.core
import docutils.nodes
import docutils.parsers.rst
class AbstractDirective(docutils.parsers.rst.Directive):
has_content = True
required_arguments = 0
optional_arguments = 0
option_spec = {}
final_argument_whitespace = False
node_class = docutils.nodes.container
def run(self):
self.assert_has_content()
text = '\n'.join(self.content)
admonition_node = self.node_class(rawsource=text)
self.state.nested_parse(self.content, self.content_offset,
admonition_node)
admonition_node.set_class("abstract")
return [admonition_node]
def setup(app):
app.add_directive('abstract', AbstractDirective)
There must be some way to add the title as well. Perhaps you need to add a
title node yourself. The documentation is lacking there, best look at the
source for
admonitions
and you will get a feel for the docutils.
With a custom text node you should be able to make up your own note directive.

Plone: generic search form for portal_catalog

I have a few Plone sites with Archetypes-based contents.
I noticed that the vanilla portal_catalog search form (manage_catalogView) allows to filter by language, portal_type (one!) and path only - since these are always available.
Thus, whenever I need a quick search by any other criteria, this involves programming, e.g. writing a throw-away Script (Python).
Is there some extension which provides a generic search form, offering all configured search indexes? E.g.:
Search for IDs
Search for Creator
Search for creation time (two fields, for min and max; one of them or both could be used)
review state (use the distinct values for selectable choices)
...
Perhaps I missunderstood your question, but you don't need external methods for a catalog search or a custom extension.
You can use a python script object and call it right from url.
Go to ZMI root, add a Script(python) using the selection field in the upper right corner. Give an id and delete the example content.
Use the examples for queryCatalog or searchCatalog you will find in http://docs.plone.org/develop/plone/searching_and_indexing/query.html
and call your script using the TEST Tab or form URL.
Example:
In ZMI Root Folder, I created a python script called my_test
catalog = context.portal_catalog
from DateTime import DateTime
# DateTime deltas are days as floating points
end = DateTime() + 0.1
start = DateTime() - 1
date_range_query = { 'query':(start,end), 'range': 'min:max'}
query = {'id': ['id_1', 'id_2'],
'Creator': ['creator1', 'creator2'],
'created': date_range_query,
'review_state': ['published', 'pending']}
results = catalog.queryCatalog(query)
for brain in results:
print brain.pretty_title_or_id()
return printed
Finally, if you set some parameters into your python script you can also pass them through URL or the TEST tab.
Hope it helps you

Mysql not all arguments converted during string formatting using python

Under a database, I am currently creating several tables and inserting several cvs files into these tables.
While I succeeded in inserting some CVS files, some files I failed.
The code I failed is as below
import sys
sys.path.append('/Users/leigh/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages')
import mysql.connector
import config
import csv
import os
dbcon = mysql.connector.connect(database=config.db, user=config.user, password=config.passwd, host=config.host)
dbcur = dbcon.cursor()
dbcur.execute('CREATE TABLE parking (Place_ID int, Parking varchar(50))')
with open('/Users/leigh/documents/RCdata/chefmozparking.csv') as csvfile:
reader = csv.reader(csvfile)
next(reader, None)
for row in reader:
dbcur.execute('INSERT INTO parking (Place_ID, Parking) VALUES(%s, "%s")' % tuple(row))
dbcon.commit()
I used the same format (same code function on another datasets with same data structure) on the succeeded ones, but this one just failed with the error written:
dbcur.execute('INSERT INTO parking (Place_ID, Parking) VALUES(%s, "%s")' % tuple(row))
TypeError: not all arguments converted during string formatting
The columns are perfectly matched and the data type cannot be wrong.
I am not very experienced in MySQL, and I have looked through the internet and still cannot find what is wrong here.
Could some one please kindly tell me what is going on?
Thanks!
// If further information is needed, please just let me know.

What is the corret syntax for using max function

Still using bloody OpenOffice Writer to customize my sale_order.rml report.
In my sale order I have 6 order lines with 6 different lead time to delivery. I need to show the maximum out of the six values.
After many attempt I have abandoned using the reduce function as it works erratically or not at all most of the time. I have never seen anything like this.
So I thought I'd give a try using max encapsulating a loop such as:
[[ max(repeatIn(so.order_line.delay,'d')) ]]
My maximum lead time being 20, I would expect to see 20 (yes well that would be too easy, wouldn't it!).
It returns
{'d': 20.0}
At least it contains the value I am after.
But; if I try and manipulate this result, it disappears altogether.
I have tried:
int(re.findall(r'[0-9]+', max(repeatIn(so.order_line.delay,'d')))[0])
which works great from the python window, but returns absolutely nothing in OpenERP.
I import the re from my sale_order.py file, which I have recompiled into sale_order.pyo:
import time
import re
from datetime import datetime, timedelta
from report import report_sxw
class order(report_sxw.rml_parse):
def __init__(self, cr, uid, name, context=None):
super(order, self).__init__(cr, uid, name, context=context)
self.localcontext.update({
'time': time,
'datetime': datetime,
'timedelta': timedelta,
're': re,
})
I have of course restarted the server many times. My test install sits on windows.
So can anyone tell me what I am doing wrong, because I can make it work from Python but not from OpenOffice Writer!
Thanks for your help!
EDIT 1:
The format
{'d': 20.0}
is, according to python, a dictionary. Still in Python, to extract the integer from a dictionary it is possible to do it like so:
>>> dict={'d': 20.0}
>>> print(dict['d'])
20.0
But how can I transpose this to OpenERP writer???
I have manage to get the result I wanted by importing functools and declaring the reduce function within the parameters of the sale_order.py file.
I then simply used a combination of reduce and max function and it works exactly as expected.
The correct syntax is as follow:
repeatIn(objects,'o')
reduce(lambda x, y: max(x, y.delay), o.order_line, 0)
Nothing else is required.
Enjoy!

Resources