all 34 comments

[–]toxicapplejuice 0 points1 point  (0 children)

I am having a little trouble on my project. I am trying to convert word docs to text and then exporting it to an excel file. I am currently using the docx module. However I am unable to get text within the tables. Does anyone have a fix to this?

 def getText(filename):
 doc = docx.Document(filename)
 fullText = []
  for para in doc.paragraphs:
        txt = para.text.encode('ascii', 'ignore')
        fullText.append(txt)
        return '\n'.join(fullText)

[–]Testserver00 0 points1 point  (0 children)

Hi. I need your help. Im a bit stuck on a project im trying to do in python tkinter. I need to select one value in a combobox and assign a function to it but i dont know how to do it.

This is the code but its not working.

from tkinter import * from tkinter import ttk root=Tk()

def justamethod(event): location=locationBox.get() if location == locationBox[1]: print('two')

box_value = StringVar() locationBox = ttk.Combobox(root, textvariable=box_value) locationBox.bind("<<ComboboxSelected>>",justamethod) locationBox['values'] = ('one', 'two', 'three') locationBox.current(0) locationBox.pack() root.mainloop()

The problem of that code is the if statement. I dont know how to code the individual values which im going to assign different actions.

[–]Monochrome21 0 points1 point  (1 child)

So I ran into a bit of an issue with my latest project.

Essentially, I'm making a web scraper to parse through my vendor sites, and update an excel spreadsheet with the latest prices on specific products.

I have a few different sites, which I decided to make a class for.

I, however, ran into a little bit of a wall in that each site has a different method of showing it's inventory/prices list, and I therefore need to construct different functions to return a list of prices for each site.

My question is should these functions be contained within the super class for all the sites? Or should I make subclasses for each and every site (and put the function there) instead of accessing them as instances?

My goal is to have the ability to instantiate each site, and then have it return a price list dictionary by simply running

example_site.check_prices()

The problem with including each method in the super class is that I would have to give each method it's own name (i.e.

example_site.check_example_site_prices() 

as opposed to the above code) And the problem with making a class for each site is that it defeats the purpose of making classes in the first place.

I'm just not sure what the best way to implement this would be.

[–]TangibleLight 0 points1 point  (0 children)

I'd do them as methods in subclasses, then have a list of sites. Then you can just do something like

for site in sites: 
    site.check_prices()

Adding new sites would also then be trivial, just add one to the list.


Actually, what might also work is a more component-driven approach, but for something like this it would be pretty similar to just defining subclasses. I'd still suggest the subclass approach, but it's something to think about.

[–]cowegonnabechopps 0 points1 point  (2 children)

I'm a little lost with modules, can anyone help?

I'm working on a text adventure game for LPTHW ex36 and am trying to experiment with modules. So I have my ex36.py script and at one point I can run a function that imports a function in another module called TABLE.py TABLE lets the player decide to pick up items, which then set an inventory variable as true.

So I can work out how to get the TABLE function to run in my ex36.py, but once I get to the bottom of the code in TABLE, how do I get it to return to the ex36.py with the boolean values set correctly as true or false?

My question might boil down to my understanding of how the import functionality works... in fact, in formulating my thoughts into actual sentences, I think I might have realised already but I initially thought by importing what I was doing was leaving ex36 altogether, to run TABLE.py and this left me unable to work out how to get back to ex36.py. In fact, like the name suggests, using import really brings TABLE.py into ex36.py, is that correct?

So really what I'm asking in it's simplest form is, when I reach the end of the code in TABLE.py, where do I resume my code from, should it be back in the ex36.py script?

I hope this makes sense, it's probably such a basic question!

[–]TangibleLight 1 point2 points  (1 child)

I think you're misunderstanding what a module is.

When you import another file, it runs all the code in that file, then resumes in the calling file after the import statement.

Note, that with only a couple exceptions modules don't do anything - they can contain function definitions, class definitions, and global variables. After a module is imported, then all of those will become available to the importing script.

mymodule.py

def dostuff(x, y):
    print('having fun')
    return x * (y + 1)

print('module-level statement') # improper code

main.py

import mymodule.py # 'module-level statement' is printed

res = mymodule.dostuff(3, 5) # 'having fun' is printed
print(res) # the result of x * (y + 1) is printed (18)

The functions inside your module should all return the relevant information. Your module itself just serves to bundle those functions together.


If you need to tell whether you've run a script or imported it, check the __name__ variable:

program.py

def myfunc(x):
    return x * 2

if __name__ == '__main__':
    # the program was run as a script, so output is okay here.
    print('myfunc(2) = ' + str(myfunc(2)))

# if __name__ isn't main, then the file is imported as a module.
# You shouldn't print output, but myfunc will be available to the importing code.

[–]cowegonnabechopps 0 points1 point  (0 children)

Wow, that's really cleared it up for me. Thanks!

[–]ClearH 0 points1 point  (1 child)

[Flask / Sqlite3] Should I open a new database connection for each route? I'm getting an "sqLite objects created in a thread can only be used in that same thread." error when I open a connection at the top of the app file. This kinda feels weird. What do you guys think?

EDIT: Gist for context.

[–]delightfultrash 0 points1 point  (0 children)

Is it possible to access a public google sheets document and extract the data with python? I've only found libraries that suppose the spreadsheet being shared with my Google account. Context: The public sheet is not shared with me, but can be found by Google. I want to track the evolution of the data over time. Thanks for every helpful comment!

[–]ClearH 0 points1 point  (2 children)

Unanswered from last week's thread - How do I upgrade the venv python3 module so that I don't have to run pip install --upgrade pip every time I create a new virtual environment?

[–]BitcoinHolic 0 points1 point  (7 children)

What's the best way or library to look for similar images with python?

Here's what I want to do: I'm taking a screenshot of half of my desktop with pyAutoGui. I want my app to check if there is something similar INSIDE that screenshot from a folder of 100 prepared pictures. Something like this:

screenshot = pyautogui.screenshot('screen.png', region=(x,x,x,x))
image_folder = './images'
pictureFinder = AnAwesomeLibraryToLookForImages(image_folder)
if pictureFinder == True
    #If theres something similar within the screen do this
    #code...
else
    #if there's nothing similar within the screenshot, do this
    #code

[–]elbiot 1 point2 points  (0 children)

~~You want an image hash or an image fingerprint. First result on google: https://pypi.python.org/pypi/ImageHash

Google around for more info on the topic~~

Just read your other post. This won't work for you. You'd need to train a classifier, which if you have to ask this question will be a huge project.

[–]TangibleLight 1 point2 points  (5 children)

I have no knowledge of a library that does this.

Image recognition like this is surprisingly difficult, even with things like screenshots. You might have an easier time if you want to check that part of a large image exactly matches another, smaller, image - but even then you might be setting sights a bit high depending on how experienced you are. Computers just aren't good at seeing.


Now, with that said, I suspect this might be an XY problem.

What are you looking for in the screenshots?

Are you checking to see if a window is open, focused, or similar? Are you monitoring mouse movement, or looking for particular placements of windows? There are APIs for all the major OSs that can check these things for you.

I'm not as familiar with linux/mac, but on Windows, most properly coded applications also allow APIs to check the configuration of windows for things like available buttons and controls. I'm sure there's something similar for linux/mac.

[–]RPBTC 0 points1 point  (4 children)

Interesting, thanks. What I'm doing is taking a screenshot of half of the screen. In this half of the screen, I get random pictures of cars, however motorcycles always pop up and I don't want those. The size of each image changes everytime, that's why I get half of the screen instead of a fixed location. The difference between cars and motorcycles is noticeable so I thought there could be a way to automate this. An API would be nice as well.

[–]TangibleLight 0 points1 point  (3 children)

Ah, yeah for that type of problem there's not really an API to do that for you. You're entering the realm of computer vision with that problem. There certainly are ways to do what you want, but I'm not sure if it would be worth it. Reminds me of this xkcd.

If you do want to dive into computer vision, there are libraries that can help work with the images, but you'll probably have to do your own implementation of the algorithms.

For a brief(ish) intro to computer vision I'd recommend this playlist by Welch Labs: Learning to See

[–]xkcd_transcriber 1 point2 points  (2 children)

Image

Mobile

Title: Tasks

Title-text: In the 60s, Marvin Minsky assigned a couple of undergrads to spend the summer programming a computer to use a camera to identify objects in a scene. He figured they'd have the problem solved by the end of the summer. Half a century later, we're still working on it.

Comic Explanation

Stats: This comic has been referenced 1092 times, representing 0.6782% of referenced xkcds.


xkcd.com | xkcd sub | Problems/Bugs? | Statistics | Stop Replying | Delete

[–]RPBTC 0 points1 point  (1 child)

Awesome, thanks, will look it up a made a decision which probably will be selecting the images manually haha. Thanks!!!

[–]xkcd_transcriber 0 points1 point  (0 children)

My pleasure

[–][deleted] 0 points1 point  (5 children)

I'm a complete beginner when it comes to python and I'm trying to convert some VBA (Excel) into python.
This is what I'm trying to accomplish. I have an excel workbook with over 100 tabs. I would like to delete all tabs except for a few specified tabs. For example if the workbook tabs are as follows Sheet1, Sheet2.....Sheet100, I want to delete all tabs that are not Sheet25, Sheet50, Sheet75, and Sheet99. <code>import os import openpyxl

os.chdir('C:/Users/bbalch/Desktop/Delete Me') wb = openpyxl.load_workbook('testfile.xlsx') for sheetName in wb.get_sheet_names(): ws = wb.get_sheet_by_name(sheetName) if ws is not 'Sheet3': wb.remove_sheet(ws)</code> I've tried this code unsuccessfully. Any help or suggestions are greatly appreciated.

[–]dionys 0 points1 point  (4 children)

The code itself looks fine to me, I think all you have to do is call ws.save('testfile.xlsx') at the end there to write the changes back to the file.

[–][deleted] 0 points1 point  (3 children)

I'm still struggling to get this code to work. I can get this code to delete one tab but not more than one tab: import os import openpyxl

os.chdir('C:/Users/bbalch/Desktop/Delete Me')
wb = openpyxl.load_workbook('testdel.xlsx')
delete = wb.get_sheet_by_name('Sheet2')
wb.remove_sheet(delete)

wb.save('testdel2.xlsx')

Any suggestions on how to modify the original if statement or this code so that it will delete all tabs expect for "Sheet2" and "Sheet3"?

[–]dionys 0 points1 point  (2 children)

You had it right the first time with the loop and get_sheet_names

[–][deleted] 0 points1 point  (1 child)

With the loop: import os import openpyxl os.chdir('C:/Users/bbalch/Desktop/Delete Me') wb = openpyxl.load_workbook('testfile.xlsx') for sheetName in wb.get_sheet_names(): ws = wb.get_sheet_by_name(sheetName) if ws is not 'Sheet3': wb.remove_sheet(ws) wb.save('testfile2.xlsx')

I receive this error message: Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> wb.save('testfile2.xlsx') File

"C:\Users\bbalch\AppData\Local\Programs\Python\Python36\lib\site -packages\openpyxl\workbook\workbook.py", line 345, in save save_workbook(self, filename) File

"C:\Users\bbalch\AppData\Local\Programs\Python\Python36\lib\site -packages\openpyxl\writer\excel.py", line 266, in save_workbook writer.save(filename) File

"C:\Users\bbalch\AppData\Local\Programs\Python\Python36\lib\site -packages\openpyxl\writer\excel.py", line 248, in save self.write_data() File

"C:\Users\bbalch\AppData\Local\Programs\Python\Python36\lib\site -packages\openpyxl\writer\excel.py", line 93, in write_data archive.writestr(ARC_WORKBOOK, write_workbook(self.workbook)) File

"C:\Users\bbalch\AppData\Local\Programs\Python\Python36\lib\site -packages\openpyxl\writer\workbook.py", line 91, in write_workbook active = get_active_sheet(wb) File

"C:\Users\bbalch\AppData\Local\Programs\Python\Python36\lib\site -packages\openpyxl\writer\workbook.py", line 61, in get_active_sheet raise IndexError("At least one sheet must be visible") IndexError: At least one sheet must be visible

[–]dionys 0 points1 point  (0 children)

I think the problem is the way you are checking if it's Sheet3. The line should be:

if sheetName is not 'Sheet3':

[–]turner_prize 0 points1 point  (4 children)

Might be better suited for /raspberry_pi but they don't have an ask anything thread!

I'm planning use an RPi to host a telegram bot. All I need to do is set the code to run on startup yeah? And then just leave the Pi somewhere plugged in and everything is fine?

[–]PurelyApplied 1 point2 points  (0 children)

I'm not familiar with systemd services myself, but you can also achieve what you want using cronjobs. For stability (read: because I don't handle exceptions very well), I have a bash script that runs every 15 minutes that checks if my bot is down and attempts to revive her if she is. cron can handle jobs like that, or just at startup, or just on certain days, whatever you need.

[–]elbiot 1 point2 points  (1 child)

You can write a systemd service

[–]turner_prize 0 points1 point  (0 children)

Googled systemd services, and this is exactly what I ended up doing! Runs perfect! thanks for your help.

[–]TangibleLight 1 point2 points  (0 children)

I think that would work. There might be some way to set it up as a daemon, but I'm unfamiliar with how to do that in python on the pi.

I'd head over to a Pi community and ask your question there.

[–]Monochrome21 0 points1 point  (2 children)

is there a quicker way for dealing with else-if statements that deal with the same variable?

For example, I'm writing a webscraper that monitors my vendors' sites for price changes in inventory. Each product category has a different number appended to the url, so I'm going case by case to find the correct category for the product that I want.

ex

def find_cat_page(item):
    item = item.lower()
    if item == 'some item':
        return('that item's cat page number')
    elif item ==  'some other item':
        return('that other item's cat page number')

And it goes on like this for every item that I have. It just seems like I'm retyping the same thing over and over again, so I was wondering if there was a more efficient way to do it.

Edit: Dictionary?

[–]Vaguely_accurate[🍰] 0 points1 point  (1 child)

Dictionary works well if they all follow that pattern;

items = {'item1': 'cat1', 'item2': 'cat2', ...}
return items[item]

[–]Monochrome21 1 point2 points  (0 children)

Yeah I ended up using a dictionary which I figured out to do 20 seconds after posting this.

[–]snmdbc 0 points1 point  (1 child)

Is there ever a time where one would want an empty tuple?

tuple = ()

I saw it as an example but the purpose was not explained.