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
sunaurus
Feb 13, 2012

Oh great, another bookah.

huhu posted:

code:
def funct(word):
    print(word)
funct(hello world)
Is there something I can add to this so that I don't have to put quotes around my string? OR is that just dumb coding practice?

I'm writing a class with dictionary.openD(filename.txt) and I'm lazy and don't want to write dictionary.openD("filename.txt")

You can store your filename in a variable.

code:
f = "filename.txt"
dictionary.openD(f)
The quotes are not just a dumb coding practice. Usually, strings are far less common than other 'elements' (varibles, keywords, etc) in code. It doesn't make any sense to assume that some parts of your code are strings without you explicitly stating that they are, and you do this by using quotation marks.

Adbot
ADBOT LOVES YOU

Cingulate
Oct 23, 2012

by Fluffdaddy
I hate how R seemingly randomly allows you to drop quotes in some places.

I also hate places in Python where you can turn something into a string without quotes, e.g. the old dict pattern

quote:

dict(becomes_a_string_key=variable,...)

Hegel
Dec 17, 2009

Don't be so sure. Just look up "bare words" for python. Doing this in Ruby is a bit easier since monkeypatching has better support.
http://stackoverflow.com/questions/29492895/bare-words-new-keywords-in-python

That said, don't ever actually do this. Supporting bare words is a fun exercise in metaprogramming, but a hideous attribute to pollute your runtime with.

OnceIWasAnOstrich
Jul 22, 2006

Cingulate posted:

I also hate places in Python where you can turn something into a string without quotes, e.g. the old dict pattern

That's not really special, you are just passing the keys to a function as kwargs in the same way kwargs are treated for every function in Python.

Nippashish
Nov 2, 2005

Let me see you dance!

Hegel posted:

Don't be so sure. Just look up "bare words" for python. Doing this in Ruby is a bit easier since monkeypatching has better support.
http://stackoverflow.com/questions/29492895/bare-words-new-keywords-in-python

I find this deeply unsettling.

Cingulate
Oct 23, 2012

by Fluffdaddy

OnceIWasAnOstrich posted:

That's not really special, you are just passing the keys to a function as kwargs in the same way kwargs are treated for every function in Python.
Good point, but they're turned into strings still!

QuarkJets
Sep 8, 2008

Cingulate posted:

Good point, but they're turned into strings still!

Yeah but I'm not really sure why it annoys you

Cingulate
Oct 23, 2012

by Fluffdaddy

QuarkJets posted:

Yeah but I'm not really sure why it annoys you
My feeble little mind has made a note that the relationship between the name of a variable referring to a string, and the string itself, is arbitrary. Here is an example where it's different.

priznat
Jul 7, 2009

Let's get drunk and kiss each other all night.
As someone who is forced to work on projects in TCL at my job this conversation is triggering me. :haw:

QuarkJets
Sep 8, 2008

Cingulate posted:

My feeble little mind has made a note that the relationship between the name of a variable referring to a string, and the string itself, is arbitrary. Here is an example where it's different.

Maybe I'm misunderstanding, but I don't think that's the case; my_dict['some_variable'] doesn't return the string 'some_variable', unless you explicitly set that up.

This might also blow your mind: every Python variable is also an entry in a dictionary, so technically every variable can also be accessed as a string. But again, the string keys in the dictionary don't refer to themselves

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Hegel posted:

Don't be so sure. Just look up "bare words" for python. Doing this in Ruby is a bit easier since monkeypatching has better support.
http://stackoverflow.com/questions/29492895/bare-words-new-keywords-in-python

That said, don't ever actually do this. Supporting bare words is a fun exercise in metaprogramming, but a hideous attribute to pollute your runtime with.

That looks quite fun as a novel way of adding support for some syntax you want for a task you often want to do at the repl.

Cingulate
Oct 23, 2012

by Fluffdaddy

QuarkJets posted:

Maybe I'm misunderstanding, but I don't think that's the case; my_dict['some_variable'] doesn't return the string 'some_variable', unless you explicitly set that up.
You're probably thinking too much about this ... I never claimed I have a rational argument against the syntax of dict().

QuarkJets posted:

This might also blow your mind: every Python variable is also an entry in a dictionary, so technically every variable can also be accessed as a string. But again, the string keys in the dictionary don't refer to themselves
Yeah, but accessing the globals dict is typically presented as bad behavior isn't it?

QuarkJets
Sep 8, 2008

Cingulate posted:

Yeah, but accessing the globals dict is typically presented as bad behavior isn't it?

Absolutely

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Cingulate posted:

You're probably thinking too much about this ... I never claimed I have a rational argument against the syntax of dict().

One of the things people tried previously to point out is that this is not something special about the dict() builtin. This is something you can do in any function. So at the risk of being pedantic, referring to this as "the syntax of dict()" is not accurate. It's merely the way dict() treats its keyword arguments.

code:
def f (**keyword_args):
    for item in keyword_args.items():
        print(item[0] + ": " + item[1])

f(cat = 'meow', dog = 'woof')

quote:

Yeah, but accessing the globals dict is typically presented as bad behavior isn't it?

The poster didn't mention the globals dict. The globals dict is not the only context in which one can access a dictionary with variables or object attributes as string keys. try this at the repl:

code:
class c:
    HELLO = None
c.__dict__
import re
re.__dict__
locals()

huhu
Feb 24, 2006
Simplified question.

code:
class Test:
    def method():
        print("hello")
    def method2():
        method()
How would I get Test.method2() to print "hello"?

huhu fucked around with this message at 20:57 on Feb 13, 2016

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

huhu posted:

Simplified question.

code:
class Test:
    def method():
        print("hello")
    def method2():
        method()
How would I get Test.method2() to print "hello"?

You could change the statement in method2() to Test.method()

Better than that (but it is potentially difficult for a beginner to appreciate why), you can make method2 a class method:

code:
class Test:
    def method():
        print("hello")
    @classmethod
    def method2(cls):
        cls.method()
The main reason why you might do this is that it's more robust if you might later want to derive from Test.

Also, I haven't fixed this in my code but method() should either have a self parameter or be marked as a static method or class method (in the latter case it should have a cls parameter instead)

Gothmog1065
May 14, 2009
Okay, I know this code is terrible in general, but there's things I might end up doing in the future with it, but I'm writing a simple ping script that will write to a text file to see if a list of computers is online. Here is what I have so far:

The csv is just a list of computer names and people who "own' them (computer,owner).

Python code:
import os

csv = open('test.csv', 'r')
new_file = "Results of computer online check:\n"

for line in csv:
    line = line.split(",")
    computer, name = str(line[0]), str(line[1])

    ping = os.system("ping -n 1 " + computer)
    print(ping)

    if ping == 0:
        ping_res = "online"
    else:
        ping_res = "offline"

    new_file += computer + " is currently " + ping_res + " and owned by " + name

csv.close

results = open('results.txt', 'w')
results.write(new_file)
results.close
Here's the issue: ping returns 0 if it gets a DNS entry (Not even a valid ping), and 1 if it doesn't have a DNS entry. Is there another way to simply ping the computer?

e: one thing I'm coming up is I should be using subprocess.call instead of the way I'm doing it.
e2: Seems subprocess.call does the same thing, I'm seeing a -c switch but I'm not sure how it works, guess it's time to learn ping more in depth!

Gothmog1065 fucked around with this message at 22:27 on Feb 18, 2016

No Safe Word
Feb 26, 2005

Python comes with a csv module which will simplify some of that

Your more general questions is that pinging by computer name will only work if you get name resolution via NetBIOS/WINS, which if you're in a domain should "just" work (or really I think even if you're just on the same subnet, I think Windows runs network discovery services for those kind of name announcements, though I can't remember for sure). The name resolution stuff is more of a network sys admin question. Hopefully they're aware of what you're doing if you're going to be randomly pinging machines on the network :v:

Gothmog1065
May 14, 2009

No Safe Word posted:

Python comes with a csv module which will simplify some of that

Your more general questions is that pinging by computer name will only work if you get name resolution via NetBIOS/WINS, which if you're in a domain should "just" work (or really I think even if you're just on the same subnet, I think Windows runs network discovery services for those kind of name announcements, though I can't remember for sure). The name resolution stuff is more of a network sys admin question. Hopefully they're aware of what you're doing if you're going to be randomly pinging machines on the network :v:

I'll definitely hit up my network guys to see the best way to check if a computer is online. It's definitely not going to be something run constantly or on a lot of computers (85 is the max right now, and 90% of them are in the same subnet). The DNS is doing something weird and losing the IPs for connected devices for some reason, it's weird, but it's probably something on the switch itself (since I don't think we're hitting the global DNS server, just what is stored on the switch for now). Either way, I'll see what my network guys come up with. Thanks!

Bozart
Oct 28, 2006

Give me the finger.

Gothmog1065 posted:

Okay, I know this code is terrible in general, but there's things I might end up doing in the future with it, but I'm writing a simple ping script that will write to a text file to see if a list of computers is online. Here is what I have so far:

...

e: one thing I'm coming up is I should be using subprocess.call instead of the way I'm doing it.
e2: Seems subprocess.call does the same thing, I'm seeing a -c switch but I'm not sure how it works, guess it's time to learn ping more in depth!

While you're at it, I would look at async and await, because blocking on network calls is for chumps. Chumps!

Flash Gordon
May 8, 2006
Death To Ming
I'm working on a project that allows the creation of a number of different plots/running various analyses from the command line. Currently the output directory where the plots are saved is fixed relative to the project directory. I'd like to have it so that the user can specify an alternate location as a command line option if they want but otherwise it would default to whatever setting is in a configuration file. Eventually, many more options need to be configurable so the settings can be used throughout the program. I'm not really sure what the best way to go about implementing some sort of global configuration system is, though.

Should I just read from the file into some object, override any settings that the user specifies on the command line and then pass the object around to any functions that need configuration? I'm just not sure how to design this in a way that won't make future development super cumbersome.

huhu
Feb 24, 2006
code:
import io
def fromFile(fileName):
    with io.open(fileName,'r',encoding='utf8') as f:
        text = f.readlines()
        print(text)
     return text
the fileName refers to:
code:
peinó
porquería
print gives:
code:
peinó
porquería
return gives:
code:
['\ufeffpeinó', 'porquería']
What is \ufeff and how can I stop it from appearing?

Asymmetrikon
Oct 30, 2009

I believe you're a big dork!
FEFF is a zero-width space. You've probably got one in your file.

huhu
Feb 24, 2006

Asymmetrikon posted:

FEFF is a zero-width space. You've probably got one in your file.

I've found this: http://stackoverflow.com/questions/17912307/u-ufeff-in-python-string

Is a zero-width space the fact that's encoded in UTF-8? And how would I go about removing that? I've tried reading the instructions listed on the page and I don't understand it at all.

Risket
Apr 3, 2004
Lipstick Apathy

huhu posted:

code:
import io
def fromFile(fileName):
    with io.open(fileName,'r',encoding='utf8') as f:
        text = f.readlines()
        print(text)
     return text
the fileName refers to:
code:
peinó
porquería
print gives:
code:
peinó
porquería
return gives:
code:
['\ufeffpeinó', 'porquería']
What is \ufeff and how can I stop it from appearing?
This should get rid of it
code:
 text = f.readlines().replace('\ufeff', '') 

Asymmetrikon
Oct 30, 2009

I believe you're a big dork!
Whoops, you're right, it's being used as a BOM. U+FEFF is for UTF-16, though; are you sure your file is UTF-8?

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug
Some tools on Windows (like Notepad, as I recall) prepend U+FEFF to files before saving as UTF-8. Though this doesn't function as a byte order mark (since the concept of byte order doesn't apply to UTF-8), it's still technically valid and serves as a marker that "this file is definitely UTF-8 content".

Python defines a special codec called utf-8-sig that strips the leading UTF-8 "signature" from the beginning of the file.

code:
$ hexdump -C hello.txt 
00000000  ef bb bf 68 65 6c 6c 6f  0a                       |...hello.|
00000009
$ python -c "with open('hello.txt') as f: print(repr(f.read()))"
'\ufeffhello\n'
$ python -c "with open('hello.txt', encoding='utf-8-sig') as f: print(repr(f.read()))"
'hello\n'

Dominoes
Sep 20, 2007

Speaking of string encoding, is there an elegant way to fix mangled ascii(?) charcters; ie something simpler and broader than this:

Python code:
title = title.replace('’', "'")  # managed right quote
title = title.replace(''', "")  # mangled apostrophe
title = title.replace('"', '"')  # mangled quote
An example function might look like this. I figured this might be something built in, but I haven't found anything.

Python code:
def fix_mangled_chars(mangled: str) -> str:
    chars = {
        '’': "\'",
        ''': "\'",
        '"': '"',
	#...
    }

    replace = curry(lambda x, y: y.replace(x[0], x[1]))
    fixers = map(replace, chars.items())
    return compose(*fixers)(mangled)

Dominoes fucked around with this message at 00:08 on Feb 22, 2016

QuarkJets
Sep 8, 2008

I really don't understand the question

SurgicalOntologist
Jun 17, 2004

https://github.com/kmike/text-unidecode/

or

https://pypi.python.org/pypi/Unidecode

KernelSlanders
May 27, 2013

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

Lysidas posted:

Some tools on Windows (like Notepad, as I recall) prepend U+FEFF to files before saving as UTF-8. Though this doesn't function as a byte order mark (since the concept of byte order doesn't apply to UTF-8), it's still technically valid and serves as a marker that "this file is definitely UTF-8 content".

Python defines a special codec called utf-8-sig that strips the leading UTF-8 "signature" from the beginning of the file.


I'm continually amazed how Windows manages to mangle files. We bought some Bing search ads and hooked up our normal analytics pipeline to their API to download some CSVs that say how the ads did. Care to guess how it broke?

qntm
Jun 17, 2009

KernelSlanders posted:

I'm continually amazed how Windows manages to mangle files. We bought some Bing search ads and hooked up our normal analytics pipeline to their API to download some CSVs that say how the ads did. Care to guess how it broke?

Line endings?

Space Kablooey
May 6, 2009


KernelSlanders posted:

I'm continually amazed how Windows manages to mangle files. We bought some Bing search ads and hooked up our normal analytics pipeline to their API to download some CSVs that say how the ads did. Care to guess how it broke?

Trick question. The API broke.

KernelSlanders
May 27, 2013

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

qntm posted:

Line endings?

Yup. Even if you work at Microsoft, who sits down to write a web endpoint and thinks CR LF termination is a good idea?

supercrooky
Sep 12, 2006

KernelSlanders posted:

Yup. Even if you work at Microsoft, who sits down to write a web endpoint and thinks CR LF termination is a good idea?

That's still better than what Excel on OSX does with csv, which is terminate with CR alone.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Dominoes posted:

Speaking of string encoding, is there an elegant way to fix mangled ascii(?) charcters; ie something simpler and broader than this:

You want to decode HTML entities. http://stackoverflow.com/a/2087433

Dominoes
Sep 20, 2007

Nailed it!

html.unescape(mangled)

Dominoes fucked around with this message at 20:00 on Feb 22, 2016

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

KernelSlanders posted:

Yup. Even if you work at Microsoft, who sits down to write a web endpoint and thinks CR LF termination is a good idea?

Wikipedia says... "Most textual Internet protocols (including HTTP, SMTP, FTP, IRC, and many others) mandate the use of ASCII CR+LF ('\r\n', 0x0D 0x0A) on the protocol level, but recommend that tolerant applications recognize lone LF ('\n', 0x0A) as well." I'm not sure whether you were downloading files rather than making web requests, but it seems to me that in that case Microsoft are quite entitled to send you CR+LF files, and if you don't account for that possibility then that's your bag.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
That's for the protocol, though, not for the body of the content. The body is a chunk of random binary data and is not line-delimited. Even if it's coincidentally also textual data, like CSV data.

Adbot
ADBOT LOVES YOU

KernelSlanders
May 27, 2013

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

Hammerite posted:

Wikipedia says... "Most textual Internet protocols (including HTTP, SMTP, FTP, IRC, and many others) mandate the use of ASCII CR+LF ('\r\n', 0x0D 0x0A) on the protocol level, but recommend that tolerant applications recognize lone LF ('\n', 0x0A) as well." I'm not sure whether you were downloading files rather than making web requests, but it seems to me that in that case Microsoft are quite entitled to send you CR+LF files, and if you don't account for that possibility then that's your bag.

Downloading files.

  • Locked thread