Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
QuarkJets
Sep 8, 2008

Hughmoris posted:

Thanks for this. It is exactly what I'm looking for but I'm having a bit of a trouble to get working. I have a script called hello_world.py.I have Python installed on my laptop at C:\Python27\python.exe. I want to make the script accessible to my coworkers on the network drive I:\Public\Scripts.

The batch file below does not appear to launch the script from the network drive:
code:
python_runtime\python27\python.exe hello_world.py
I substituted C:\ for "python runtime" and it executes from my computer but it doesn't execute from other computers because they don't have access to my c drive. Not sure if I'm explaining that right.

What am I missing?

Edit:
Do I need to install Python to the network drive? Not sure if the IT guys will be a fan of that. :smith:

Yes, he's saying that you'll need to place the python.exe somewhere that's executable by other people.

Adbot
ADBOT LOVES YOU

FAT32 SHAMER
Aug 16, 2012



What is the preferred IDE for Mac? I know that I can technically use NetBeans, and I think I've heard of a way to use Xcode with it, but I'd rather not find out the hard way and just start learning the best one immediately.

SurgicalOntologist
Jun 17, 2004

PyCharm!

EAT THE EGGS RICOLA
May 29, 2008

PyCharm owns.

David Pratt
Apr 21, 2001

Hughmoris posted:

Do I need to install Python to the network drive? Not sure if the IT guys will be a fan of that. :smith:

Pretty much this. You should be able to take a copy of the python runtime you've got installed locally and just copy it onto the network share. I don't see why this would be a problem for IT unless they're idiots (they're idiots, aren't they?).

You can't just take python.exe - you need the standard library files and all the other gubbins too.

namaste friends
Sep 18, 2004

by Smythe
Another vote for pycharm, Mac or PC.

KernelSlanders
May 27, 2013

Rogue operating systems on occasion spread lies and rumors about me.

Cultural Imperial posted:

Another vote for pycharm, Mac or PC.

4

FAT32 SHAMER
Aug 16, 2012



Thanks guys! :unsmith:

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

PyCharm 4 EAP came out today.

FoiledAgain
May 6, 2007

regularizer posted:

I'm trying to figure out python syntax, and one of the codecademy exercises is to write a function that removes all duplicates of integers in a list, so if you have [1,1,2,2,3] it returns [1,2,3].


I know you're trying to learn, so writing the big function is a good idea, but the easiest way to handle this is just:

ints = list(set(ints))

That turns your ints first into a set type, which will automatically remove the duplicates for you. Then it converts the set into a list, and you're back to where you want to be.

regularizer
Mar 5, 2012

FoiledAgain posted:

I know you're trying to learn, so writing the big function is a good idea, but the easiest way to handle this is just:

ints = list(set(ints))

That turns your ints first into a set type, which will automatically remove the duplicates for you. Then it converts the set into a list, and you're back to where you want to be.

Gotcha, thanks. I was trying to learn list comprehensions before the tutorial got to it. On a related note, when I finish the codecademy tutorial in a few days what should I do next? I'd like to get a more in-depth understanding of python before moving on to another language. I was thinking about trying to make a simple program I could run on osx, so is there something I could do to learn about making GUIs with python?

Dominoes
Sep 20, 2007

regularizer posted:

Gotcha, thanks. I was trying to learn list comprehensions before the tutorial got to it. On a related note, when I finish the codecademy tutorial in a few days what should I do next? I'd like to get a more in-depth understanding of python before moving on to another language. I was thinking about trying to make a simple program I could run on osx, so is there something I could do to learn about making GUIs with python?

You should start a project of whatever you want, then look things up or ask questions when you approach a problem you don't understand. For me at least, it's easier to stay motivated that way than learning academically.

Qt (PyQt or Pyside) is probably the best GUI for Python, but it's a pain to learn, with no good tutorials.

coaxmetal
Oct 21, 2010

I flamed me own dad
Instead of using pycharm, you can always customize the poo poo out of vim like a cool person

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

coaxmetal posted:

Instead of using pycharm, you can always customize the poo poo out of vim like a cool person

PyCharm is so awesome though...why would you do that to yourself

Chosen
Jul 11, 2002
Vibrates when provoked.
In general I've found that people who don't prefer PyCharm don't yet know what it has to offer.

Ahz
Jun 17, 2001
PUT MY CART BACK? I'M BETTER THAN THAT AND YOU! WHERE IS MY BUTLER?!
The more I use PyCharm, the more I like it.

Except when my VM is brought back from suspension and my running Django has pegged my CPU and I have to kill Java. Aside that.

FoiledAgain
May 6, 2007

regularizer posted:

I was thinking about trying to make a simple program I could run on osx, so is there something I could do to learn about making GUIs with python?

Python comes with tkinter, which you can use to build a GUI. It has its limitations, of course, but then what doesn't. I use it regularly, and I generally consult http://effbot.org/tkinterbook/.

edit: wow, I just realized that says it hasn't been updated since 2005. But then, I don't think that Tk has been updated for a long time either. In any case, that site still has useful relevant information.

FoiledAgain fucked around with this message at 20:50 on Sep 23, 2014

sadus
Apr 5, 2004

Only a couple weeks into my Pythoning but I'm loving PyCharm too. With a little wrangling I got PyCharm running in Windows to auto-deploy to my Linux box, and then when I hit "run" it runs python on the remote machine to test my package, wheeeee.

Question: How can you access the exception object inside an 'except'/catch in Python 3.4? Using subprocess in Python 3.4, I'm trying to catch the CalledProcessError exception, which is supposed to give me returncode/cmd/output. But "except subprocess.CalledProcessError, e:" is apparently now invalid syntax so I can't access "e.output" etc.

-e nevermind saw an example a page or two back, "except subprocess.CalledProcessError as e:" is working, yay.

sadus fucked around with this message at 20:55 on Sep 23, 2014

accipter
Sep 12, 2003
I use PyCharm from time to time, but I mostly use Vim because that is what I have been using for the longest time. One of my frustrations with PyCharm is that everything has to be in a project, which seems a little strange when I have a script that does one relatively simple thing.

Dominoes
Sep 20, 2007

accipter posted:

I use PyCharm from time to time, but I mostly use Vim because that is what I have been using for the longest time. One of my frustrations with PyCharm is that everything has to be in a project, which seems a little strange when I have a script that does one relatively simple thing.
Yea that's my beef with PyCharm too, and is why I keep Notepad++ around.

OnceIWasAnOstrich
Jul 22, 2006

I used to use TextWrangler for individual little scripts but I missed all of the PyCharm features too much. I just open whatever folder that script is in as a project. It doesn't take any more effort that opening that file directly, and the only clutter it created is the .idea folder which ends up invisible to me, and since I put that in my global .gitignore I don't even have to worry about git picking it up. I mostly end up keeping all of my random scripts in the same utility script folder which is my most commonly used PyCharm project anyway.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

OnceIWasAnOstrich posted:

I used to use TextWrangler for individual little scripts but I missed all of the PyCharm features too much. I just open whatever folder that script is in as a project. It doesn't take any more effort that opening that file directly, and the only clutter it created is the .idea folder which ends up invisible to me, and since I put that in my global .gitignore I don't even have to worry about git picking it up. I mostly end up keeping all of my random scripts in the same utility script folder which is my most commonly used PyCharm project anyway.

Yeah, this is how I handle it as well.

KernelSlanders
May 27, 2013

Rogue operating systems on occasion spread lies and rumors about me.

regularizer posted:

Gotcha, thanks. I was trying to learn list comprehensions before the tutorial got to it. On a related note, when I finish the codecademy tutorial in a few days what should I do next? I'd like to get a more in-depth understanding of python before moving on to another language. I was thinking about trying to make a simple program I could run on osx, so is there something I could do to learn about making GUIs with python?

The answer to that depends entirely on why you are learning python. If it's to have fun, then pick something (simple, always start simple) that you think would be fun to try to build it. It really doesn't matter what it is. If you're learning python to get a job, then it depends on what kind of job: general developer, web developer, front-end developer, data scientist all suggest different classes of projects you should try.

good jovi
Dec 11, 2000

'm pro-dickgirl, and I VOTE!

regularizer posted:

Gotcha, thanks. I was trying to learn list comprehensions before the tutorial got to it. On a related note, when I finish the codecademy tutorial in a few days what should I do next? I'd like to get a more in-depth understanding of python before moving on to another language. I was thinking about trying to make a simple program I could run on osx, so is there something I could do to learn about making GUIs with python?

Maybe it's just my web background talking, but the patterns involved in writing desktop apps are totally incomprehensible to me. What you're thinking seems to be a really common desired first step, but I'd suggest taking a smaller one. I'm a big fan of the static blog generator as a "learn the language" project. I think the problems involved are a lot easier to grasp, and won't get in your way as much while still trying to figure out how class inheritance works (for example).

Or as KernelSlanders said, solve a problem that you actually have. There's no better way to stay motivated and interested in a project.

Murodese
Mar 6, 2007

Think you've got what it takes?
We're looking for fine Men & Women to help Protect the Australian Way of Life.

Become part of the Legend. Defence Jobs.
Python is hell for GUI apps, which is probably why most popular Python apps with a GUI use Angular or something and have a web frontend instead. This is possibly also why nobody can be bothered writing a decent multiplatform GUI library.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Could someone school me a little bit on regex?

I've been teaching myself regex and learned how to look for a match. What I want to do now is if a match is found, replace the match with something else.
For example, a text file contains this:
code:
test test asOIS=123^^
ajasdfOIS=123^^ test test
What I want to do is replace all '123' found with '9999' so the text file would read:
code:
test test asOIS=9999^^
ajasdfOIS=9999^^ test test
Any ideas on how to best tackle this?

Edit:
Maybe it would be easier to read in the very long text file as a string, and then do a string.replace('123', '9999')?

Hughmoris fucked around with this message at 00:22 on Sep 26, 2014

Alexei
Aug 16, 2007

Hughmoris posted:

Could someone school me a little bit on regex?

I've been teaching myself regex and learned how to look for a match. What I want to do now is if a match is found, replace the match with something else.

Take a look at the "re" module in the standard library, and check out re.sub() function. That should do what you're looking for. :)

I don't know if one is more efficient than the other, but I think re.sub() and string.replace are fairly similar at least in terms of how much code you'd need to write.

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Alexei posted:

Take a look at the "re" module in the standard library, and check out re.sub() function. That should do what you're looking for. :)

I don't know if one is more efficient than the other, but I think re.sub() and string.replace are fairly similar at least in terms of how much code you'd need to write.

Thanks. I discovered I'm able to handle string.replace much easier than regex, so I went that route. I then read up on CSVs. I now have a script that is going to save me a ton of time on a small work project...

I have CSV that has rows of path name, old item number, new item number. My boss was expecting me to manually open up each file and swap the item numbers. Now, I've hacked together a hideous program that will iterate through the CSV, grabbing each pathname and opening that file, then replacing the old item number with the new item number, then move on to the next file.

I'm so happy I've learned the basics of scripting. :smith:

Lyon
Apr 17, 2003

Hughmoris posted:

Thanks. I discovered I'm able to handle string.replace much easier than regex, so I went that route. I then read up on CSVs. I now have a script that is going to save me a ton of time on a small work project...

I have CSV that has rows of path name, old item number, new item number. My boss was expecting me to manually open up each file and swap the item numbers. Now, I've hacked together a hideous program that will iterate through the CSV, grabbing each pathname and opening that file, then replacing the old item number with the new item number, then move on to the next file.

I'm so happy I've learned the basics of scripting. :smith:

Are you parsing the csv with regex too? Were you the one who parsed HTML with regex as well? Python has a standard library csv module for working with csv and same thing for html/xml. Regex is kind of ugly for both of those cases.

It sounds like it is working so might as well just run with it, but csv and html/xml are better to parse with libraries built for those purposes rather than creating your own 'parser' with regex.

Congrats on the script though, it is awesome when you take what would have been a horrible manual process and automate the hell out of it. Don't tell your boss and take a week to deliver these results while reading the forums and relaxing hah :).

Lyon fucked around with this message at 03:14 on Sep 26, 2014

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Thanks.

I'm using the CSV module for this. I am the one who was trying to parse XML with regex and I was able to get the job done. I've grown frustrated and put learning APIs to the side, I can't seem to learn how to extract meaningful data from them. I can make the API call and receive the XML or JSON data but when I try to extract information using any of the parsers it turns out to be a mess.

Here is my proof-of-concept code for the csv script. If there is an obvious way to handle it better, I'm all ears:

code:
#this script will iterate through a CSV with multiple rows and three columns. The filepath is in column 0.
import csv 
r = csv.reader(open('C:/projects/python/test_data.csv')) #read in CSV as CSV object
for row in r: #iterates over each row in CSV. So for this, row[0] is file name
	f = open(row[0]).read() #read in the file path
	f = f.replace('OIS=1234', 'OIS=9999')
	new_file = open(row[0], 'w')
	new_file.write(f)
	new_file.close()

Lyon
Apr 17, 2003

Hughmoris posted:

Thanks.

I'm using the CSV module for this. I am the one who was trying to parse XML with regex and I was able to get the job done. I've grown frustrated and put learning APIs to the side, I can't seem to learn how to extract meaningful data from them. I can make the API call and receive the XML or JSON data but when I try to extract information using any of the parsers it turns out to be a mess.

Here is my proof-of-concept code for the csv script. If there is an obvious way to handle it better, I'm all ears:

code:

#this script will iterate through a CSV with multiple rows and three columns. The filepath is in column 0.
import csv 
r = csv.reader(open('C:/projects/python/test_data.csv')) #read in CSV as CSV object
for row in r: #iterates over each row in CSV. So for this, row[0] is file name
	f = open(row[0]).read() #read in the file path
	f = f.replace('OIS=1234', 'OIS=9999')
	new_file = open(row[0], 'w')
	new_file.write(f)
	new_file.close()

Phone posting but that looks pretty good to me, I thought you were using regex to parse the files and was a bit concerned. Since you're using csv I take back my above comments!

Space Kablooey
May 6, 2009


Hughmoris posted:

code:
#this script will iterate through a CSV with multiple rows and three columns. The filepath is in column 0.
import csv 
r = csv.reader(open('C:/projects/python/test_data.csv')) #read in CSV as CSV object
for row in r: #iterates over each row in CSV. So for this, row[0] is file name
	f = open(row[0]).read() #read in the file path
	f = f.replace('OIS=1234', 'OIS=9999')
	new_file = open(row[0], 'w')
	new_file.write(f)
	new_file.close()

Is it working on your end? Isn't opening two file pointers to the same file wrong?


EDIT: Stupid bold tags don't work inside code.

Space Kablooey fucked around with this message at 14:22 on Sep 26, 2014

Hughmoris
Apr 21, 2007
Let's go to the abyss!

HardDisk posted:

Is it working on your end? Isn't opening two file pointers to the same file wrong?


EDIT: Stupid bold tags don't work inside code.

It works fine for me, no errors or warnings popped up. Maybe someone smarter than me can explain how the two file pointers work because I don't have a clue.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Hughmoris posted:

code:
#this script will iterate through a CSV with multiple rows and three columns. The filepath is in column 0.
import csv 
r = csv.reader(open('C:/projects/python/test_data.csv')) #read in CSV as CSV object
for row in r: #iterates over each row in CSV. So for this, row[0] is file name
	f = open(row[0]).read() #read in the file path
	f = f.replace('OIS=1234', 'OIS=9999')
	new_file = open(row[0], 'w')
	new_file.write(f)
	new_file.close()

You're not closing f.

Python code:
import csv 

r = csv.reader(open('C:/projects/python/test_data.csv')) #read in CSV as CSV object
for row in r: #iterates over each row in CSV. So for this, row[0] is file name
    
    with open(row[0]) as f:  # The `with` statement creates what is called a context manager
        contents = f.read()
    
    # When you exit the `with` block, f.close() is called automatically
    with open(row[0]) as f:
        f.write(contents.replace('OIS=1234', 'OIS=9999'))

BabyFur Denny
Mar 18, 2003
I am trying to extract text from a pdf using pdfminer and I run into the following problem:
code:
Traceback (most recent call last):
  File "C:\Program Files\python\lib\site-packages\pdfminer3k-1.3.0-py3.4.egg\pdfminer\pdfparser.py", line 765, in read_xref
    pos = self.find_xref()
  File "C:\Program Files\python\lib\site-packages\pdfminer3k-1.3.0-py3.4.egg\pdfminer\pdfparser.py", line 724, in find_xref
    raise PDFNoValidXRef('Unexpected EOF')
pdfminer.pdfparser.PDFNoValidXRef: Unexpected EOF
The function in pdfparser.py that causes the error>
code:
def find_xref(self):
        """Internal function used to locate the first XRef."""
        # the word 'startxref' followed by a newline followed by digits
        re_startxref = re.compile(r'startxref\s*[\r\n]+\s*(\d+)', re.MULTILINE)
        # try at the end, then try the whole file.
        m = re_startxref.findall(self.data, len(self.data)-4096)
        if not m:
            m = re_startxref.findall(self.data)
        if not m:
            raise PDFNoValidXRef('Unexpected EOF')
        logging.debug('xref found: pos=%r', m[-1])
        return int(m[-1])
The weird thing is that I can find several results if I search the PDF for that regular expression manually. Is the PDF broken or what could be the issue? I didn't find any conclusive help on the internet.

Jose Cuervo
Aug 25, 2004
I need some help understanding scope in Python. I have two functions

Python code:
def func_one(bool_var):
    bool_var= True

def func_two(my_dict):
    my_dict['a']= 1
If bool_var is initially False, a call to func_one does not change the value:

Python code:
bool_var= False
func_one(bool_var)
print bool_var
results in False being printed.

However, if my_dict is initially empty, a call to func_two results in a non-empty dictionary:

Python code:
my_dict= {}
func_two(my_dict)
print my_dict
results in {'a': 1} being printed.

Why does func_one not result in a change but func_two does?

namaste friends
Sep 18, 2004

by Smythe
Jose Cuervo, I don't fully understand it myself but these two articles helped:

http://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference
http://www.jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither/

SurgicalOntologist
Jun 17, 2004

Jose Cuervo posted:

I need some help understanding scope in Python. I have two functions

Python code:
def func_one(bool_var):
    bool_var= True

def func_two(my_dict):
    my_dict['a']= 1
If bool_var is initially False, a call to func_one does not change the value:

Python code:
bool_var= False
func_one(bool_var)
print bool_var
results in False being printed.

However, if my_dict is initially empty, a call to func_two results in a non-empty dictionary:

Python code:
my_dict= {}
func_two(my_dict)
print my_dict
results in {'a': 1} being printed.

Why does func_one not result in a change but func_two does?

The first one can be explained something like this:
  1. Assign the name bool_var to the False object, in the __main__ scope.
  2. Call func_one passing it the object that bool_var points to, False.
  3. (within func_one) Assign the name bool_var to the object passed in, in the func_one scope.
  4. (within func_one) Assign the name bool_var to the True object, in the func_one scope. The name bool_var in the __main__ scope is not affected, nor is the False object that it still points to and that bool_var within the function used to point to.
  5. Print the object that bool_var points to in the __main__ scope.

And the second one:
  1. Create an empty dictionary and assign it the name my_dict, in the __main__ scope.
  2. Call func_two, passing it to the object my_dict points to.
  3. (within func_two) Assign the name my_dict to the object passed in, in the func_two scope.
  4. (within func_two) Assign the object 1 to the item 'a', in the object my_dict refers to in the func_two scope (still the same object it refers to the in the __main__ scope).
  5. print the object my_dict refers to in the __main__ scope.

In both, at step 3, bool_var and my_dict in the inner and outer scopes point to the same object.

The difference is that in the first one, step 4 reassigns the name within the function scope, having no effect on the names in the outer scope, or any of the objects they point to. After step 4, bool_var in and out of the function no longer point to the same thing.

In the second one, step 4 modifies the dictionary that is pointed to by my_dict in both scopes.

It's the difference between reassigning a name and modifying an existing object.

SurgicalOntologist fucked around with this message at 17:49 on Sep 28, 2014

QuarkJets
Sep 8, 2008

Jose Cuervo posted:

I need some help understanding scope in Python. I have two functions

Python code:
def func_one(bool_var):
    bool_var= True

def func_two(my_dict):
    my_dict['a']= 1
If bool_var is initially False, a call to func_one does not change the value:

Python code:
bool_var= False
func_one(bool_var)
print bool_var
results in False being printed.

However, if my_dict is initially empty, a call to func_two results in a non-empty dictionary:

Python code:
my_dict= {}
func_two(my_dict)
print my_dict
results in {'a': 1} being printed.

Why does func_one not result in a change but func_two does?

Python variables are passed by assignment. Lists and dictionaries are mutable, so you can modify one of those in-place even when you pass that object to a function. But if you were to assign a new list to that variable name, within the scope of a function, then the list outside of that function would be unchanged.

In this case, you're creating a new bool object and passing the variable assignment to the function. Within the function, you have access to that assigned value. However, when you assign a new value to the same variable name within the function, that does not effect the variable of that name outside of the scope of the function.

So if you did this:
Python code:
def func_three(some_dict):
  some_dict = {'a':1}

my_dict = {}
func_three(my_dict)
you'd have the same result as func_one: the variable is assigned to a new object within the scope of the function, but outside of the function nothing has changed.

tl;dr Variable assignments within a function do not effect variables of the same name outside of that function. But if you modify that assigned value (by invoking one of its methods to modify its contents, like you do in func_two), then that does effect variables pointing to that value outside of the function.

Adbot
ADBOT LOVES YOU

Pudgygiant
Apr 8, 2004

Garnet and black? More like gold and blue or whatever the fuck colors these are
I started with Exercism today, it's pretty cool so far, got some helpful feedback about readability within about an hour of putting up my first submission. I'm still relatively a beginner, but I figured I could help out people like this guy
code:
def hey(what):
    what = what.strip()
    if what == "":
        return "Fine. Be that way!"

    def lastChar(string): return string[len(string)-1]
    char = lastChar(what);

    if what.isupper():
        return "Whoa, chill out!"
    if char == '?':
        return "Sure."
    else:
        return "Whatever."
by dropping hints about bool instead of checking equality. I wanted to add something about speed, which got me curious just how much it would speed it up, when I found this...
code:
>>> mean(timeit.repeat('bool(x)', "x=''", repeat=100))
0.10749417543411255
>>> mean(timeit.repeat('not x', "x=''", repeat=100))
0.025772578716278076
>>> mean(timeit.repeat("x==''", "x=''", repeat=100))
0.03028071641921997
The only relevant thing I found on Google was this StackExchange discussion that wasn't really ever resolved. Any ideas what the hell's going on there?

  • Locked thread