|
Spaseman posted:Can someone explain how to make the line "BaconTest001.Pit001" in the "AreaOne" class run? I'm working on deepening my understanding of classes and their interctions but nothing I do can make this particular line run. I know the code is a mess but that was the point. Theres more, and deeper, issues that I was too lazy to fix, but maybe this will help. Python code:
|
# ? Aug 9, 2015 17:06 |
|
|
# ? May 9, 2024 00:34 |
|
Thermopyle posted:Theres more, and deeper, issues that I was too lazy to fix, but maybe this will help. This is incredibly helpful and has made many things clearer but I do still have two questions. First, I noticed that for in the init of BaconTest you used self.pit and self.stream while in the areaonetest method of AreaOne you didn't. What is the significance of that? Second, is it possible to create an object in CLASS_A and then edit that same instance in a different class? EDIT:I wanted to clarify this second question a bit. I know you can pass an object from one object to another for editing but can I call an object without passing or is that something that just isn't done? Spaseman fucked around with this message at 21:15 on Aug 9, 2015 |
# ? Aug 9, 2015 21:07 |
|
Spaseman posted:First, I noticed that for in the init of BaconTest you used self.pit and self.stream while in the areaonetest method of AreaOne you didn't. What is the significance of that? It signifies laziness on my part. Spaseman posted:Second, is it possible to create an object in CLASS_A and then edit that same instance in a different class? Always pass stuff to code that needs stuff. Otherwise you're dealing with a bunch of global state that is just terrible. I'm a little unclear where your confusion is, so I'll throw some ugly code at you. This isn't exactly how I'd do this, but maybe it'll clarify something for you? Python code:
|
# ? Aug 9, 2015 22:04 |
|
Thermopyle posted:It signifies laziness on my part. It clears up a lot for me, thank you, but I do have one more question. Say the fondle method of ClassB had an attribute not located in the init that I wanted to access and change. What code would you write to do that? Or is that something that just isn't done?
|
# ? Aug 9, 2015 23:36 |
|
Spaseman posted:It clears up a lot for me, thank you, but I do have one more question. Say the fondle method of ClassB had an attribute not located in the init that I wanted to access and change. What code would you write to do that? Or is that something that just isn't done? Methods don't have attributes (well they do, but it's not applicable and isn't commonly used). I assume you mean how the fondle method is accessing self.fondle. You can access any attribute you want on self. It doesn't matter if it was assigned to in __init__ or in another method, or whatever. Have some more code exploring some of this: Python code:
|
# ? Aug 10, 2015 00:03 |
|
I just dabble in Python so someone shout at me if I'm going wrong here... but as far as I'm aware, your scopes are basically dictionaries, so any attributes you use are effectively dictionary entries. Trying to access b.rubbed before it's been assigned is really looking up a key that isn't in the dictionary yet. So assigning your attributes in init means you're adding all the valid entries to your dictionary at the beginning, and anything that accesses them will get a value back. You can add them later, like how rub() assigns a rubbed entry, but then you have an issue with the dictionary's current state - if you try to get the value of that entry, has it actually been added yet? That can make it hard to reason about the actual state of the object, which is why it's easier to just create its attributes at construction time
|
# ? Aug 10, 2015 00:19 |
|
baka kaba posted:I just dabble in Python so someone shout at me if I'm going wrong here... but as far as I'm aware, your scopes are basically dictionaries, so any attributes you use are effectively dictionary entries. Trying to access b.rubbed before it's been assigned is really looking up a key that isn't in the dictionary yet. So assigning your attributes in init means you're adding all the valid entries to your dictionary at the beginning, and anything that accesses them will get a value back. Pretty spot on explanation there. code:
|
# ? Aug 10, 2015 01:05 |
|
I just wanted to say thanks to everyone for the help, especially Thermopyle. This has cleared up a lot of my misconceptions about classes and Python in particular. I finally feel like I have a firmer grasp of what I am doing in Python.
|
# ? Aug 10, 2015 02:57 |
|
This discussion got me thinking about a more philosophical question about using classes. I'm writing a routine to pull data of various types from the same place and put them somewhere else. The core mechanics are: - For each data type, run the query function with the appropriate arguments - Once the lists are gathered, check the data lists against what I have, pass a list back of the data I don't already have (for each type) - Download new data The way I'm approaching it is within the main function (i.e. if __name__ == 'main') it takes the arguments from the user (which specify which data types they wants to update), and run the queries to be eventually passed to a downloader. I'm just dumping it into a larger dictionary. So the structure of that dictionary is like: code:
I'm not really clear what the "best" way to do this is. I guess this may just be a personal preference thing, but I'm also trying to future proof this a bit if that matters.
|
# ? Aug 10, 2015 19:55 |
|
Is there a favourite user agent parser? All the ones I've googled for seem to be slightly out of date, they don't seem to detect windows 10 and microsoft edge for example.
sharktamer fucked around with this message at 10:28 on Aug 12, 2015 |
# ? Aug 12, 2015 10:18 |
|
sharktamer posted:Is there a favourite user agent parser? All the ones I've googled for seem to be slightly out of date, they don't seem to detect windows 10 and microsoft edge for example. Good.
|
# ? Aug 12, 2015 16:37 |
|
sharktamer posted:Is there a favourite user agent parser? All the ones I've googled for seem to be slightly out of date, they don't seem to detect windows 10 and microsoft edge for example. http://www.useragentstring.com/pages/api.php
|
# ? Aug 12, 2015 17:27 |
|
sharktamer posted:Is there a favourite user agent parser? All the ones I've googled for seem to be slightly out of date, they don't seem to detect windows 10 and microsoft edge for example. Don't do that.
|
# ? Aug 12, 2015 18:12 |
|
Given this spec for communicating with a treadmill over serial, what data type should I be sending? I've never done this kind of thing before. I thought it should be b'C0' for example, but that doesn't work. Do I need to do some kind of encoding? I'm a bit out of my element here. I'm also not entirely confident I've got the device/communication set up correctly with pyserial. I do get code:
when I power-cycle the treadmill, but after that I don't seem to get anything, even the error codes, no matter what I send in.
|
# ? Aug 13, 2015 06:38 |
|
Blinkz0rz posted:Don't do that. Yeah it's a stupid task, given all the browsers lie about who they are. I know it's better to check for browser capabilities or that type of thing, but this is what I've been asked to do.
|
# ? Aug 13, 2015 09:55 |
|
SurgicalOntologist posted:Given this spec for communicating with a treadmill over serial, what data type should I be sending? I've never done this kind of thing before. I thought it should be b'C0' for example, but that doesn't work. Do I need to do some kind of encoding? I'm a bit out of my element here. Is that all the spec you have? It looks like not really enough. They should also provide you the unit's desired baud rate and parity settings. To test the communication, you should be able to write 0xA4 and read out 0xB4. Python code:
|
# ? Aug 13, 2015 13:58 |
|
KernelSlanders posted:Is that all the spec you have? It looks like not really enough. They should also provide you the unit's desired baud rate and parity settings. There's another page of commands, and a cover sheet, but I think that stuff is at the top of the page I linked. Baudrate is 4800, no parity. KernelSlanders posted:To test the communication, you should be able to write 0xA4 and read out 0xB4. Ah, I didn't know there were hex literals. I'll try this later today. KernelSlanders posted:Remember, though, any time you have moving parts controlled by a computer you need to have an emergency stop button that physically disconnects power from the unit. University labs like to skip this step, but it can go really, really badly. It comes with its own emergency stop button that overrides the computer control. Plus one of those cord things you put around the person's wrist so it stops if they fall. Thanks for the advice!
|
# ? Aug 13, 2015 15:14 |
|
i'm still in the process of learning python at an intermediate level and i am running out of ideas of what to make. where can i get some good ideas for things to build and contribute to github? something actually useful, too.
|
# ? Aug 13, 2015 18:49 |
|
Figured out the bytes thing. It had to be dev.write([0xA1]) (i.e. it expected an iterable).
|
# ? Aug 13, 2015 19:11 |
|
Fractale posted:i'm still in the process of learning python at an intermediate level and i am running out of ideas of what to make. where can i get some good ideas for things to build and contribute to github? something actually useful, too. I was just wanting a little side project so I'm writing a thing for scraping my banks website. It's probably against their ToS or whatever, but it's been enjoyable figuring out how their website works and writing the right code to scrape it and setting up nice looking classes and well structured code.
|
# ? Aug 13, 2015 20:04 |
|
Fractale posted:i'm still in the process of learning python at an intermediate level and i am running out of ideas of what to make. where can i get some good ideas for things to build and contribute to github? something actually useful, too. there's myriad things you can do. kinda depends what you're interested in. ofc scraping seems pretty popular. graphics (vispy), game stuff, audio synthesis (dsp) or parsing, django Web stuff, procedural crap, chat clients, encryption or secure things... networking , number crunching (?? ???) mathsy poo poo ,algos (fortunes algorithm I'm working on atm for example) .. learn concurrency like asyncio or twisted .. Some thing like decorators if u don't know that poo poo.. stuff to automate ur life, track poo poo.. plugins? esolang interpreter ,nlp/nlg like I said depends what ur into mononvc fucked around with this message at 09:13 on Aug 14, 2015 |
# ? Aug 13, 2015 21:14 |
|
Spaseman posted:A class is the constructor or blueprint that is used to create instances that can be edited but the constructor itself is not actually changed in any way, correct? Not quite but close Think of it as the difference between a concept and an object. The class *defines* something. The object is an *instance* of it. Dog is a class. Fido is an object of type Dog. A constructor is specifically code that runs when the an instance of the class is made. Its a function that sets the new object up. Conversely a destructor pulls it down, but we dont really use destructors in python typically.
|
# ? Aug 13, 2015 23:32 |
|
SurgicalOntologist posted:Figured out the bytes thing. It had to be dev.write([0xA1]) (i.e. it expected an iterable). Yeah that makes sense. Glad you got it working.
|
# ? Aug 14, 2015 02:45 |
|
It's been a bit since I've used Python for CSV parsing, so this might be a stupid design issue I'm overlooking: I have a CSV file formatted like this: pre:time value par frame 1 111 1.1 h 1.12 e 1.13 y 1. Iterate over the rows and build a list of entries in a list 2. Somehow concatenate the entries in the list to make a human-readable string Is there some idiomatic way I'm overlooking? I'm thinking it's a simple iteration problem, but it seems like there is a more efficient way I'm not thinking about.
|
# ? Aug 15, 2015 02:39 |
|
KernelSlanders posted:Yeah that makes sense. Glad you got it working. Thanks! Controlling a treadmill from the command line is kind of cool. Fluue posted:It's been a bit since I've used Python for CSV parsing, so this might be a stupid design issue I'm overlooking: I think what you want is this pandas operation. Although you will have to get a column that has all the same values of time. Python code:
code:
Python code:
SurgicalOntologist fucked around with this message at 03:25 on Aug 15, 2015 |
# ? Aug 15, 2015 03:22 |
|
Continuum guys: how do you like it over there
|
# ? Aug 15, 2015 09:37 |
|
Fluue posted:It's been a bit since I've used Python for CSV parsing, so this might be a stupid design issue I'm overlooking: import pandas as pd df = pd.read_csv(csv_filename) print("".join(df["value"])) Sure, you could do it without pandas, but if you ever deal with csv, you're gonna use pandas eventually.
|
# ? Aug 15, 2015 11:38 |
|
Cingulate posted:Sure, you could do it without pandas, but if you ever deal with csv, you're gonna use pandas eventually. What does pandas do that the built in csv module doesn't ? I just did a small project working with a set of csv files and dictreader handled the job perfectly.
|
# ? Aug 15, 2015 18:41 |
|
The Fool posted:What does pandas do that the built in csv module doesn't ? (Also, I don't understand how I managed to miss SO's post ...)
|
# ? Aug 15, 2015 19:34 |
|
SurgicalOntologist posted:
Yep this was perfect. Thanks for the intro to Panda, it'll be handy when doing more CSV parsing later.
|
# ? Aug 16, 2015 01:44 |
|
I want to write a really simple python application, that is listening for certain keypresses (arrow keys, space bar) in the console, and based on input executes a function. It's a 2 wheeled R2D2 type robot. initial loading parameters so far is code:
code:
code:
code:
code:
Hadlock fucked around with this message at 08:42 on Aug 17, 2015 |
# ? Aug 17, 2015 08:30 |
|
Curses will do that yeah, it's in the stdlib.
|
# ? Aug 17, 2015 14:40 |
|
I'm working with VMware's vRealize API and to provision VMs I need to send it a pretty hefty chunk of JSON. I figured if I could just make a class I could stuff in an import and just have it build the JSON for me then I could avoid my actual scripts that build VMs having shitloads of JSON in them. I'm basically trying to assemble a class I can feed a dict of values and have it spit out the JSON. I'd want usage to look something like this: Python code:
Python code:
|
# ? Aug 19, 2015 18:17 |
|
What does that gain you over just using a function?
|
# ? Aug 20, 2015 01:19 |
|
Second SurgicalOntologist's question about why not just use a normal method call. I also have to ask why you have so much boilerplate data in the build_input method. Wouldn't it be cleaner and more maintainable to put that into a config or something, and read that into the class when you instantiate it?
|
# ? Aug 20, 2015 01:32 |
|
Emacs Headroom posted:Second SurgicalOntologist's question about why not just use a normal method call. Probably so, I have no idea what I'm doing if I'm honest. I can't find much info on the best ways to do these things and the closest analogue to what I want is using XML but doesn't make much sense to me (I can't find XML templates for that particular library anywhere in the tree, it just seems like magic) The big reason that ugly mess of JSON is in the class right now is I'm not 100% sure the best way to just stuff it in a file somewhere and then easily replace the fields I want with the values I want. When I noodled on it it seemed like I might need to use something like jinja2 which I'm open to but i just want to make sure I'm in the right ballpark. As to why a class and not just a function; there's a few methods I left out that were relatively straightforward to me (related to managing catalog items) and I wanted the option potentially recycle the object elsewhere. It's also partially an exercise to get a better understanding of when/where to use classes. I'm actually replacing functions that currently exist and trying to get a better understanding for how https://github.com/vmware/pyvcloud/ works. It's pretty much my first class beyond the samples people would copy/paste from a couple google searches.
|
# ? Aug 20, 2015 05:41 |
|
1000101 posted:I'm actually replacing functions that currently exist and trying to get a better understanding for how https://github.com/vmware/pyvcloud/ works. It's pretty much my first class beyond the samples people would copy/paste from a couple google searches. Ah I see. Well tbh, I'm not sure this code is the best example of the way to do production Python. In particular, there are a lot of magic strings all over the place, including urls, which is annoying to maintain. A couple of principles I could see helping are: (1) avoid magic values wherever possible (2) try to make explicit data schemas as much as possible Using Avro or something like that is probably overkill (although it would give you some real benefits if this is to be production-ized), but one simple option could be to actually put all that magic boilerplate into a json-formatted file, with all the missing fields set to null. Then refactor your class to expect a dictionary for the skeleton output json (maybe check that it contains expected keys). Nix your current property annotations, and just have a single non-__init__ method called 'format_request(self, request_dict, output="json")' or something. Have that method deepcopy the skeleton json, then slide in the new values from the request_dict, and return the json-serialized result.
|
# ? Aug 20, 2015 14:04 |
|
Emacs Headroom posted:Ah I see. Well tbh, I'm not sure this code is the best example of the way to do production Python. In particular, there are a lot of magic strings all over the place, including urls, which is annoying to maintain. I appreciate this insight as I really start to get lost trying to follow that code (I really just need to start looking at standard library code instead of poo poo from vendors I'm integrating the products with). I've never heard the term 'magic strings' or 'magic values' before so I'll go poking around to understand that more to avoid doing it in the future. I think I'm going to go ahead with what you're suggesting. To make sure I understand you right, dump the actual json somewhere in the module path (say in a templates directory); when I need it, read it in as a dict then just set the values I need like I was setting them in a dictionary. Then return the final JSON just like before. I'm also going to read about Avro since that sounds kind of interesting. It won't make this go-round but maybe in 6 months.
|
# ? Aug 20, 2015 18:49 |
|
I very often find myself doing something like this:code:
code:
|
# ? Aug 20, 2015 19:26 |
|
|
# ? May 9, 2024 00:34 |
|
things, stuff = zip(*list_of_tuples)
|
# ? Aug 20, 2015 19:35 |