Python3.5

pastebin.com/m3a8uhPP

>Started to learn about python for 2 weeks ago(no school :/)
>Just figured out class + function
What do you think I could do better?

Other urls found in this thread:

tutorialspoint.com/execute_python3_online.php?PID=0Bw_CjBb95KQMRndvaEY5Y0MtTWc
docs.python.org/3/library/functions.html
pastebin.com/m3a8uhPP
pastebin.com/vhcSvcLY
twitter.com/AnonBabble

learn C

I know vb.net

But C# or C++?

both

I ain't reading this.

Explain what it does and I'll write a version that's one tenth the LOC.

tutorialspoint.com/execute_python3_online.php?PID=0Bw_CjBb95KQMRndvaEY5Y0MtTWc

Run the script online

quite possibly the shittiest code I've ever seen

holy shit

1989 called, they want their language back.

Then fucking tell me how to make it like
10 times shorter

look up "hash table"

I'm not going to spoon feed you, git gud faggot

>blemishing a functional progamming language with objects
OOP is what you do on paper before you start coding, OOP languages are literally the WYSIWYG editors of programming

>All these fucking directories
>Files with literally a single line
Ever heard of XML, OP?

>XML
>hash table
All noted, I will learn

>absolutely_unused_variable
It's like I'm back in uni.

You're doing what is commonly referred to as "hardcoding".

certainly no better than these digits

Capital letter names for classes, I think.

Besides, "data" is about the most terrible name for a class. Even "MyData" would be better.

There are so many ways you fucked up in that code holy shit. Please excuse me, I would help but I don't even know where to start. Just look up on the internet about how to write not only working code, but also correct and simplified code. You could easily cut your code size in half with correct coding methods.

>2 weeks
What the fuck user. Just do the official Python tutorial, you should've learned classes and functions in the first afternoon.

i dont know if you'll read this but heres what you should fix.

- you have lots of global variables. you should not use these. put them inside a function, and if you need the variable somewhere else you can pass it as an argument. eg:

- line 7, python convention is to name variables in lowercase using underscores. so you would call this: name_of_script (or even better, call it script_name)

def func(number):
print("this is a number:", number)


def main():
n = 4
func(n)

- line 28, you dont need to split this string into multiple lines. right now its ugly. usually we wrap lines at 78 characters across.


- line 37. classes should have names in capitals. so call it Data.

- line 38. these are bad variable names. they don't describe what they do.

- line 57, just like with variables, functions and methods (thats what we call functions when theyre inside a class) should be named in lowercase using underscores. so you would call this: get_navn

- line 75. 'type' is the name of a builtin function. look here:
docs.python.org/3/library/functions.html

you should never name a variable the same as any of these.

- you should look into the 'with' structure: it opens a file for you, and closes it when you're finished with it. basically everytime you open a file (which happens a lot in your code), you should use 'with'.

- line 119, this 'pass' isnt doing anything.

- line 122. remember that when you're programming something very repetitive like this (you must've done a lot of copy pasting here), you can always get it done by looping. heres a replacement for lines 126 to 135:


directories = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

for directory in directories:
os.makedirs(dir(directory))

you can fix the rest of the function in a similar manner.

- line 159, this is silly putting return here and above if either path from the if-statement gets to return. so you can delete the 'return' from line 158 and 160, and replace line 159 with return.

- line 195, you shouldn't use global. global variables are difficult to debug and lead to all sorts of problems, so they should be avoided unless absolutely necessary.

and by the way, all these can be done on 1 line:
global a,b,c,d,e,f,g,h,i,j


line 217: you definitely shouldn't have all this here. python should not execute any code as it reads through the file like this, it should get to the bottom then run some sort of 'main' method which starts the program. so lines 217 to 227 should go in a function,
lets call it read_all_data, and then your main function calls it:


def read_all_data():
checkDir()
a = readData("a")
# and so on.....


def main():
read_all_data()


main()

- line 231: this is very ugly. i would do this as follows:

return_str = "-' * 20 # string now contains whats on line 231

return_str += "\n"+ headerName +":\n"

# then from here on you should use some sort of loop to concatenate to the string.

# and at the end:

return(return_str)

- line 259, its usually simpler (to think about) if booleans are 'positive'. so instead of 'close', call this 'open'. then the next line would be:

while open:


which is easier to read.


- theres lots of problems with main, but if you make those changes i mentioned to all places that are applicable, it should be a lot better.

feel free to ask any questions

>feel free to ask any questions
who are you and why do you think Veeky Forums is an appropriate place for this kind of shit

take it to fucking

Man
Thanks for the re-play

>pastebin.com/m3a8uhPP (Before)
>pastebin.com/vhcSvcLY (After)

this
neither.
C# is a meme and C++ is the stupidest language there is.

>C++ is the stupidest language there is.

jesus OP

I used to write code like that too

since it has just been 2 weeks it isf ine

but trust me keep that code saved somewhere and look at it in 2-3 years and cringe

OP, don't buy into the Python meme. You're getting memed out of employability quite fucking hard

Learn C++. Just trust me

Python is good for small tasks like plotting and data manipulation. If you want to do something tha requires heavy numerical load, you should use C or Fortran basically. You do not need anything else apart from this three, It's all the same really.

you do not want C with its shit safety for number crunching

fortran or Python with Numpy/Cython all the way.

Pretty wrong tbqh. Python is used so extensively in Data Science that it's basically the lingua franca. The back end of the packages are written in C, of course, but there is literally no reason to actually write your program in C, even if you have an enormous "numerical load," as you put it, unless the algorithm you've developed is fundamentally different from existing tools and you have to implement everything from the ground up.

I am not a computer scientist and just following the status quo atm. I really want to write my algorithms using numpy, but it's not recommended or encouraged by my peers.
So, I get really chickened out whenever I decided to write an inversion algorithm or so. Should I say fuck you to Fortran and use numpy all the way.
.

Just do what you want, who gives a shit. Python is fine and numpy is based on fortran any way. Also it's not really about learning one language or the other, just learn to program is what matters.
t. employed python dev

I might be mistaken, but it looks like some functions (e.g. check_if_dir_exist) randomly return True as if they had to return something at all cost, and that value isn't used at all.
?

Also don't know if you may use this, but all chars have an index and e.g.
[chr(i) for i in range(97,104)]
are your letters.

You can get the index with the function ord, and if you declared a value, like
var = 3
then you can work with the variable names as string and evaluate the string via eval.
E.g.
var = 3
print eval('var')
will try to evaluate var, which is can because I set it to 3.

Pretty much this. I work in meteorology / satellite observations and here in the UK, lots of institutions are already (and have been) making the move from IDL etc. towards Python. My colleagues at JAXA and NASA are doing the same. We've acquired several projects which explicitly stated Python as a requirement; so I'm lucky to lecture half the bloody department on how to efficiently use Python for data analysis.

Heavy number crunching, whatever that means, can be done in Python as well, as long as you stick to array operations via Numpy - and that covers the vast majority of computation scenarios. Sure, it'll never be as fast as a nifty Fortran routine, but how much time are you losing because you're stuck with a 4000-line piece of code just to parse some input text files to control you main program?

Who knows what's going to happen 20 years from now, but as things stand now - getting to grips with Python is a safe bet for the next couple of years.

>old languages are bad
>C++ still lacks features from fucking 1950s programming languages
>the lisp family has actually moved on since then and seppleskids are still playing with their tinker toy templates

yeah dude literally no one outside of academia uses fortran for anything but legacy code. learning on a lower level language is useful but knowing python is much more useful.

Seriously it's 2016, literally no one except 60+ year old professors and their victims- I mean students uses fortran, they really shouldn't be allowed to teach it like it's a standard. My grad numerical analysis professor used fortran, thank fuck he let us use matlab on the final projects, every other class used python or let us pick, well I guess stats used R but R is also not retarded and the syntax is a bit better than numpy for lin alg stuff.

If you professionally use Python, how far is Pandas spread?

I do GUI programming wiht Python a few ours a week, and have a payed data analysis project the rest of the week, where I'm trying out Pandas now, and also Jupyter. It's not much computing, just finding correlations and making graphs.

(PS if it were up to me, I'd force static type systems upon everything, simply because I like math and logic, not "human needs" code like web apps, system programming and all that stuff you actually would get paid for)

Pandas is used a lot for time series analysis, but I'd say it's still not a well known framework, although it is indisputably a very potent one.

I tend to use its nifty csv/hdf conversion capabilities, but I haven't had any time-series analyses related tasks lately.