How to search for ZCatalog object names - python

I want to Search for an Object name.
If i have this Structure:
/de/myspace/media/justAnotherPdf.pdf
Then i want to Search for the name "justAnotherPdf" to find it or something like "justAnot"
I have Indexed the pdf files.
But i cant search it with TextIndexNG2 or PathIndex.

Currently this is not supported out-of-the-box. Object identifiers (getId) are only indexed as field values and thus can only be looked up as whole strings.
You'd need to add separate index to the catalog to support your use-case. You could add a new TextIndexNG2 index with a new name indexing just the getId method. In the ZMI, find the portal_catalog, then it's 'Indexes' tab, then on the right-hand-side you'll find a drop-down menu for adding a new index. Pick a memorable name ('fullTextId' for example) and use getId as the indexed attribute.
You'll need to do a reindex, but only for that index. Once added, select it in on the Indexes tab (tick the check-box) and select 'Reindex' at the bottom of that page. Now you can use this index in your custom searches with a wildcard search.

import os.path
name = os.path.splitext(os.path.split(url)[1])[0]
explaining the code:
from os.path import split, splitext
url = '/de/myspace/media/justAnotherPdf.pdf'
path, name_with_ext = split(url)
name_without_ext, ext = splitext(name_with_ext)

Related

Updating Autocad block attribute values of specific attribute tags using Python

I have a Autocad drawing (used as a template) in which I have a certain amount of instances of the same block. My goal is to write a script in which I can select the object(s) based on a list of handle ID's then for all of those instances (of the same block) I would like to update the values of a (or multiple) specific attribute tags.
E.g. of block(handle_id=12345) update value for TAG_1 to X and value of TAG_2 to Y.
The problem is for each object, I have found a way to get a list of the attributes, but not specify which attribute I want to edit.
import win32com.client
app = win32com.client.Dispatch("AutoCAD.Application")
Entity = app.ActiveDocument.HandleToObject('12345')
for attr in Entity.GetAttributes():
if attr.TagString == "Surface area wall":
attr.TextString = NEW_VALUE
attr.Update()
if attr.TagString == "Height wall":
attr.TextString = NEW_VALUE2
attr.Update()
Instead of iterating over the list of tags, I'd like to be able to specify which tag I want to update the value of.

How to delete document from index by it's path in Whoosh

First i add documents to index like this:
writer.add_document(title=doc_path.split(os.sep)[-1], path=doc_path, content=text, textdata=text)
And then i just need to delete one of them completely from index by it's path. Documentation says there are few no low level method to do this:
delete_by_term(fieldname, termtext)
Deletes any documents where the given (indexed) field contains the
given term. This is mostly useful for ID or KEYWORD fields.
delete_by_query(query)
Deletes any documents that match the given query.
but i can't find suitable and very convenient method for me where i can specify path of the document and just remove it. There is some low level method where i can specify internal doc_number, which i supposed to get somehow.
Can anyone give me advice how it's better to accomplish this task?
ix = open_dir('/my_index_dir_path/..')
writer = ix.writer()
writer.delete_by_term('path', doc_path)
writer.commit()
delete_by_term
method does exactly what i need. Note, that first argument is a text string 'path', and them goes the actual path. My mistake was to put an actual path instead of attribute name.

Use dictionary as lookup table

I have a jinja template that I want to pass a value into (an identifier for a country). The format of the data is a two letter country code (for example "PL" for Poland).
Through the template, I need to pass the corresponding flag as an output, but the flag is saved in the app folder structure, so i need to get the path to the image.
My problem: I could not figure out a way to use os.path in jinja, so now im trying to solve it by creating a dictionary that matches country string and relative path like this:
countries = {"PL" : "countries/flags/poland.png"}
where the system path to the app folder gets added afterwards in Python through os.path.
My question: How can i use the country string I am getting to automate transformation into the path format of the country? Something like:
for data in countries:
if data in countries.keys:
return countries.value
Thanks in advance!
Assuming data is a country code (like "PL" for example):
def get_path(data):
return countries.get(data)
The get() method checks if the dictionary has the key, and if so returns the corresponding value, otherwise it returns None.
If you want a default value other than None when the key is not present you can specify it as second argument, like this:
def get_path(data):
return countries.get(data, "default/path/x/y/z")

How to get the element name with a hyphen on a Spyne WebServer?

I am using Spyne to create a WebService from an existing WSDL (we can not change any element names, etc.).
I am facing this issue that one element name has a hyphen.
The code fragment is below:
class RequestType(ComplexModel):
_type_info = [
('Book_Name', Unicode(min_occurs=1)),
('orderedCount-totalCount', Unicode(min_occurs=1)),
......
The code
print(RequestType.orderedCount-totalCount)
raises this error:
AttributeError: 'RequestType' object has no attribute 'orderedCount'
It seems Python does not understand the hyphen character.
I can not change the element name because of strict name rules, which are required by the existing WebClient.
Is there any way to access the value from this element in Spyne/Python?
Can we read values based on their order instead of their names?
You can use getattr(inst, 'orderedCount-totalCount')

Python search for value in string using wildcards

I'm trying to make an address book for a school project, but I can't get my head around searching for values when part of the value is queried.
Here is the block of code at which I am stuck on:
self.ui.tableWidget.setRowCount(0)
with open("data.csv") as file:
for rowdata in csv.reader(file):
row = self.ui.tableWidget.rowCount()
if query in rowdata:
self.ui.tableWidget.insertRow(row)
for column, data in enumerate(rowdata):
item = QtGui.QTableWidgetItem(data)
self.ui.tableWidget.setItem(row, column, item)
As you can see, I'm using a PyQt TableWidget to display search results from a csv file. The code above does work, but it only displays the result when a full query is given. The line of code that checks for a match is:
if query in rowdata:
So for example, if I wanted to find someone called John, I would have to search for exactly "John". It wouldn't appear if I searched "Joh" or "john" or "hn" and so on...
If you require more information, just ask :P
What I think you want to do is search for query within the fields of your rows, not the whole row at once.
The problem you're having is that string in obj does different things depending on what type obj has. If it is a string, it does a substring search (like you want). If it is a non-string container however, it does a regular membership check (which is why it only finds exact matches now).
So to fix it, change your test from if query in rowdata to if any(query in field for field in rowdata). If you only want to search for matches within a specific field (like the contact's name) then it could be even simpler: if query in rowdata[name_column] (where name_column is the number of the column of the name field is in your CSV file).

Categories

Resources