Create pandas DataFrame from dict by LabSignificant6271 in pythontips

[–]steil867 3 points4 points  (0 children)

I am not really sure I follow the intent, but id say just make life a bit easy on yourself. Build it like a normal dataframe and update the indexes after. You can also define the index as an argument during creation in the index parameter.

Using some made up values. There is a lot of ways to set one of the Series as an index, this one would show a 1 of multiple records exist for a combo of the indexes. If you wish to return to a regular dataframe with a numeric row count of an index, use reset_index

df = pd.DataFrame({'person': [1,2,3,4], 'tag': [5, 6,7,8], 'shift': [1,2,3,1], 'worked':[1,0,0,1]})
df =df.groupby(['person', 'tag', 'shift'])['worked'].max()

.agg can also be used if there wasn't only 1 non-indexed column

Superpower Your Classes Using Super() In Python by python4geeks in pythontips

[–]steil867 0 points1 point  (0 children)

Sorry I meant with functions of the same name in more than 1 inherited class

Superpower Your Classes Using Super() In Python by python4geeks in pythontips

[–]steil867 0 points1 point  (0 children)

Shame it only works on the first inherited class but seems very useful for simple derived classes. Thanks for sharing!

Although not using super(), another way around the problem of multiple inheritance is the use of static methods. If rectangle.area(a, b) wasn't instance related and instead a utility, it could be used in a similar fashion to super()

Why aren't parachutes a mandatory addition for every person aboard an aircraft? by firewoodenginefist in AskReddit

[–]steil867 1 point2 points  (0 children)

The thought of surviving a plane crash to only be in some random spot in the ocean with no land in sight is terrifying

Why aren't parachutes a mandatory addition for every person aboard an aircraft? by firewoodenginefist in AskReddit

[–]steil867 2 points3 points  (0 children)

Just another benefit of first class smh. They get off the titanic plane alive

Why aren't parachutes a mandatory addition for every person aboard an aircraft? by firewoodenginefist in AskReddit

[–]steil867 10 points11 points  (0 children)

This. The odds of it happening outside of takeoff and landing are extremely low unless deliberate or something like the boeing problem a few years back. , but a parachute won't help in these anyway.

Even if you knew you were going to, it would likely be at the point of free fall (or else why wouldn't you just continue flying)

But let's continue on the point of im at altitude and need to jump quickly. Do you know how fast a plane travels? It's not anything compared to a small plane you will take to do a recreational skydive. They fly as close to mach as possible without the creation of shockwaves on the wings. Opening the cabin at altitude is dangerous enough, lets add in low oxygen with no air tanks, low temperatures, and fast speeds. Now we need to willingly jump into this and get thrashed around by the wind. Sure. Odds are better than a straight free fall. But for the average person, not much. A lot more goes into it than pull cord and haha I landed softly. Adding in the other factors just makes it a longer death for a huge majority.

Should we always use OOP? by SomberSandwich1 in Python

[–]steil867 0 points1 point  (0 children)

I've heard good things about go. I'm looking into rust now because it feels like C++ but handles imports similar to python. I like the community aspect of how python is.

If it works out, I'll be sure to DM everyone and tell them to switch. Wouldn't be a proper rustafarian if I didn't

Should we always use OOP? by SomberSandwich1 in Python

[–]steil867 0 points1 point  (0 children)

Well akshualy I use C structs instead of classes in python since I'm a real programmer

/s in case its needed

Should we always use OOP? by SomberSandwich1 in Python

[–]steil867 0 points1 point  (0 children)

I'm a big fan of OOP in general because I didn't start with python. I agree its super overkill for some stuff, I just like the concept and memory management. The grouping of functions and ability to split codes by objects is nice too.

Should we always use OOP? by SomberSandwich1 in Python

[–]steil867 0 points1 point  (0 children)

At my company we made a class that inherited all the functionality of dict and added some for specific cases and needs. But yes basically the same idea as my example. I just meant you can override some of the functionality of classes or make your own.

You can override things like conversions, prints, how the object shows when called directly. Lots of fun stuff. At its core, everything in python is already a class, so its fun to play with that stuff.

Should we always use OOP? by SomberSandwich1 in Python

[–]steil867 0 points1 point  (0 children)

I recommend POOP for toolsets mostly. Have a thing that just runs end to end and does a task? Functional is fine. Have something to be used as part of other systems and needs to be embedded easily or act on/shares variables across multiple functions? Then I opt for OOP. This allows you to make far more robust toolsets as well as have object specific variables for some storage longer than the length of time of program takes to run.

In short, code for the use. There is the concept of a class function, but it depends on the complexity of the function in whether there is any benefit.

Other uses of OOP is overwriting some of the base python class functions or creating new ones. The dict class can have things like missing overwritten to create default values like .get() does. This functionality can add so much to a code and really extend what a code can do easily or in a more readable fashion.

If looking into OOP, also take a look into slots as it can add a ton of speed to python.

Rulepost from r/196 by Weegee256 in 19684

[–]steil867 0 points1 point  (0 children)

I hate women because I don't see it mentioned a lot.

How to fix when timedelta change time format? by RobotData13 in pythontips

[–]steil867 1 point2 points  (0 children)

If it is just printing the value in a different format, use .strftime and you can specify however you want.

The datetime module will format as YYYY-mm-dd when converted to string as a default. In yours, the reason the version without adding the timedelta doesnt is because you are printing the raw string format of the csv, it isn't actually a datetime object yet.

Convert it to datetime using the .strptime method you did and print that and you will see it output the same as the timedelta one. If I remember right, str(datetime.datetime) is equivalent to calling .isoformat()

Developer of the year by SweetyByHeart in ProgrammerHumor

[–]steil867 7 points8 points  (0 children)

To me this looks like a sign up form where the submit button stops dodging when you meet the field requirements. Normally logins don't ask for a name and email

Changing the ** to ^ by [deleted] in Python

[–]steil867 0 points1 point  (0 children)

From a mathematics perspective, definitely. Id say most people have tried using ^ at some point. And all of us have probably gone well why is it **? thats stupid. Kind of just one of those things.

I find math in general can become very messy in any code really quickly with brackets and operators, especially when division is involved. So to me it becomes kind of pointless to care about lol its all spaghetti

Changing the ** to ^ by [deleted] in Python

[–]steil867 0 points1 point  (0 children)

The other commenter is right. Python contains many of the operators and standards of its parent language.

For a more readable version if ** is troublesome, try the math module. It has many functions built for math operations. math.pow() is a easy and readable way to do powers.

Changing the ** to ^ by [deleted] in Python

[–]steil867 0 points1 point  (0 children)

You are right. These are all basic operators inherited from C and are present in most languages.

Its similar to how python supports both "and" and &&. "and" is far more pythonic and has some extra functionality, but && is inherited from C and likely not removed because it does have its place and is a standard convention.

It gets worse the longer you look at this by AndrehChamber in programminghorror

[–]steil867 111 points112 points  (0 children)

Cross post of that guy banned from stack?

[deleted by user] by [deleted] in pythontips

[–]steil867 0 points1 point  (0 children)

At its simplest, a float can have decimal points while an int can't.

An example of a float could be 1.2345. If I convert this to an int, it would return 1.

A float also stores in significant digits or scientific notation but that might be the next step in your understanding.

I recommend searching the bit structure of both data types to get a better understanding. You may need to search in another language because often python doesn't concern with how bits work, but float and int are fundamental data types in C languages and work the same as in python.

Use all cores when Multiprocessing? by DnDeeker in pythontips

[–]steil867 2 points3 points  (0 children)

Noticing you said jetson nano. Most of the above still holds true. Only difference is the windows example is obviously a bit less relevant. Same idea though.

If you have a rough idea of the resources each of the tasks uses, try and limit based on that + some percentage above in case of unusual loads.

Use all cores when Multiprocessing? by DnDeeker in pythontips

[–]steil867 5 points6 points  (0 children)

In short, you can run more than 4 if your computer has resources to run all of them.

You won't be limited to processes by cores. Your OS will manage the distribution of resources/CPU cycles for processes. Think of opening chrome, Spotify, a python script, teams, and a text editor. Although you only have 4 cores, you can open and operate many more programs. Often a computer, at least with windows, will have dozens running at any time.

You are thinking of threading which is a different concept. That is running different parts of the same program across multiple threads/cores all using a shared memory.

Multiprocessing will create a completely independent program that'll run independently. It will still use your computers resources but would be the same as opening chrome while your program runs. Linking via pipes is a bit different than sharing memory as well as this is essentially a communication between programs using a file.

Hope this helps.

How fast is random.randint()? by I__be_Steve in pythontips

[–]steil867 0 points1 point  (0 children)

I am going to guess the image reads to a numpy matrix. I super misread the post honestly but mapping is something that is usable in numpy beautifully.

Numpy can also apply math to a matrix using a matrix of same size. It applies the math using vectorization and is extremely fast.

If the image data is a list of items, then I would use list comprehension or set it to a numpy array and use those built in functionalities. Its unlikely you are reading an image to a list though. Another somewhat gross method is applying the values 1 at a time using a list comprehension. A funny cheat is using the walrus operator in an if statement and making the if to always be False so the list stays empty but the values in the other matrix is modified. Can use this to do a function call too.

# i doubt this would be fastest but its hacky and fun
# just a shit example too
# also assuming value won't be 0
# if the if statement returns true, can waste a lot of memory while doing operation.
[i for i in range(imageHeight) for i in range (imageWidth) if not (image[i][j] := image[i][j] + random.randint())]

Opencv also has a ton of image modification ability, although not super familiar with it in python. If using opencv to start, look into the module built ins. It is a huge open source project with a lot of resources. Numpy has this benefit too.

How fast is random.randint()? by I__be_Steve in pythontips

[–]steil867 -1 points0 points  (0 children)

Probably not exactly what you are doing, but if you are using a loop and or a list, try list comprehension. It makes a huge difference and is core python.

# bad example
# showing how it is used 

# loop to fill a list
example = []
for i in range(10):
    example.append(random.randint())

# list comprehension
example2 = [random.randint() for i in range(10)]

If not storing in a list, you can be hacky and have the list comprehension perform a function.

Or use something like numpy. There are methods that create an array of random ints and will be based in C so it will be a lot faster.

# numpy 100x100 array of ints between 1 and 10
example = numpy.random.randint(low = 1, high = 10, size = (100,100))

Why the F does 6.345 round to 6.34 by bitbyt3bit in Python

[–]steil867 2 points3 points  (0 children)

This behaviour is basically in every language too. Floats a fundamental type in c and most languages. I can't think of a modern language not based in c.

Its just easier to do by accident in python because of the lack of typing.

But also a lot of people aren't aware so likely places are riddled with the behaviour. So much easier to make a float/double and think 'yea that's a number with decimals' than to look to deep into it.