r/learnpython • • Apr 27 '21

Do programmers save chunks of code for repeated use?

For example if I had an amazing figure template on matplotlib that I might want to use again is there a repository where you should put these things ?

457 Upvotes

163 comments sorted by

621

u/Rude_Order Apr 27 '21

That's pretty much all they do. I refuse to solve the same problem again when I've already solved it satisfactory the first time.

300

u/[deleted] Apr 27 '21

As long as I can remember I solved it before... and where...

121

u/DrazGulX Apr 27 '21

Incoming txt files that are named:" SOLVED THIS SHIT FROM PROJECT X", "RECYCLED CODE", "I FORGOT WHAT THIS DOES".

1:1 me and my txt files, I should clean them...

86

u/[deleted] Apr 27 '21

/* Come back later and document properly when sober. */

73

u/[deleted] Apr 27 '21

Programming high: great ideas, subpar execution.

3

u/SuperMadCow Apr 27 '21

/* HAX gonna fix this later, but works for now lol */

1

u/vardonir Apr 27 '21

I just took over as the main programmer for a project that got passed from one guy to another guy and then to me.

One bit of the code is hacked together and the git commit message basically says "it works for the demo."

2

u/dirtycimments Apr 27 '21

Good to know this also happens to other people lol. I chuckled

7

u/hanazawarui123 Apr 27 '21

I just upload these to a seperate repo on GitHub, usually as code rather than .txt

7

u/lps2 Apr 27 '21

Snippets in gitlab for small functions, projects for larger ones is my approach

2

u/hanazawarui123 Apr 27 '21

Tbh I have never used gitlab lol

4

u/[deleted] Apr 27 '21

github and gitlab are interchangeable in this context

2

u/mywan Apr 27 '21

I like to save them in an .INI file so I can fold them down to the section name used to identify them.

1

u/databassplayer Apr 28 '21

...or write a new script to clean them, using their own code

9

u/TSPhoenix Apr 27 '21 edited Apr 27 '21

This is why I go overboard with the keywords/description on functions.

That way I can just throw a bunch of words into my code search and it'll probably find what I'm looking for.

2

u/spark706 Apr 27 '21

What are we talking about?

3

u/TarumK Apr 27 '21

Do you also just do a ton of copy pasting of pre-solved stuff? I imagine there's almost never a reason to implement a well known algorithm from scratch right?

10

u/pleatedzombus Apr 27 '21

No need to copy paste. Turn a function into a library and then you just call the library.

6

u/flyingpinkpotato Apr 27 '21

Where do you host the library?

6

u/alkasm Apr 27 '21

PyPI or GitHub or both.

4

u/hugthemachines Apr 27 '21

I have thought a bit about this.. if I ever change employer, that means technically am not allowed to use the same exact code, right? At least if it is a big chunk.

18

u/Diggy696 Apr 27 '21

What mama dont know...wont hurt her.

8

u/[deleted] Apr 27 '21

Depends on the contract you signed when you joined

4

u/Ran4 Apr 27 '21

Almost certainly. Though it's possible that your employer publishes the code in a permissive license.

187

u/notarobot1020 Apr 27 '21

Oh course, but also..... the whole internet is a goldmine for snippets like stack overflow

0

u/[deleted] Apr 27 '21

[deleted]

200

u/Tanmay1518 Apr 27 '21

When you don't understand the code and break your project

29

u/plasticluthier Apr 27 '21

Copy code, replace last curly bracket, profit? Got it.

6

u/[deleted] Apr 27 '21

At work we had a server guy take a powershell script from the internet and punch it into our prod environment.

Long story short, a lotta backups were restored that day, and a few servers were lost forever and had to be rebuilt. Nuking a storage cluster will do that.

Hopefully he learned that understanding what you execute is kinda important. To this day, our team is hesitant to prefer VMs over appliances because we don't trust this other team not to nuke them.

4

u/elus Apr 27 '21

Production access and code execution protocols might need to be reviewed at that point.

5

u/[deleted] Apr 27 '21

Oh we're public sector so there's no fancy things like basic version control or execution protocols here...it's seat of your pants all day everyday. New guy on server team? He's a domain admin!

Yeah, I'm trying to find my way out this year :P

28

u/dearAbby001 Apr 27 '21

Stack overflow explains how to do the code. It’s the equivalent of watching a math video online. You will still need to know what you’re trying to do and what you’re looking for then apply the solution to your specific problem and your specifically meaningful variables and documentation.

6

u/Sw429 Apr 27 '21

I never outright copy code from stack overflow. I only ever use it to find how someone else solved the problem, and apply the same principles to mine.

28

u/SpaceZZ Apr 27 '21

Sorry to say that but I guess you don't understand how SO works. They provide very small snippets which solve very particular problem in some way. They do not have whole programs or big chunks of code. Meaning you cannot "copy" someones else project. You could do that through GitHub, download someones code, slap your name on it and compile and put out there. That's plagiarism.

-36

u/OFIndy Apr 27 '21

This is wrong. If you copy any code from SO, you should acknowledge it.

Personally, if its so few lines that you naturally change the variable names to fit your program - then I don't. But anything more I always cite.

Source: I'm an academic and also a programmer.

27

u/coder155ml Apr 27 '21

You cite every function call you lookup on SO? Lol

11

u/yuxbni76 Apr 27 '21

Academia is a hellworld so probably.

9

u/Sw429 Apr 27 '21

That must be a long citations section. "and this user showed me how to find the length of a string in python when I couldn't remember the syntax. And this user showed me how to print floats formatted to two decimal places. This user showed me how to define a class. And this user showed me how to find the length of a string when I forgot what the first answer said."

3

u/coder155ml Apr 27 '21

Haha. I mean I don’t copy blocks of code anyway, I sometimes use it just to check the syntax of built in functions or a specific library which doesn’t fall under the license anyway. I have a feeling almost nobody cites code like that

17

u/SpaceZZ Apr 27 '21

This made me courius. Where do you "cite" you snippets? In the code itself? But then noone has access too it, unless u open source?

In some additional readme txt that you ship with you code? You cite authors or you cite SO questions? And if so, do you also provide link and date when the snippet was copied? It could have changed in the meantime. Or do you link Web archive?

So many questions.

6

u/IlliterateJedi Apr 27 '21

I think it's pretty common to include the SO answer url in a function/module's docstring. I've seen this in several modules I've read through that were pulled from pip.

1

u/SpaceZZ Apr 27 '21

Haven't seen any package with urls in the function do strings. This would be against all clean code principles as well and you would need to maintain urls in the comments. Horror. Could you print me to the example?

-14

u/OFIndy Apr 27 '21

Formally, you should seek input from a lawyer.

Personally - I place a link to the any text copied from SO. When auditing the code before releasing it, I gather the links and make appropriate decisions about what to do. In some cases it is sufficient to add some disclaimers in the readme and licence files. In other cases it's more complex. See a lawyer for more details. This isn't legal advice.

8

u/[deleted] Apr 27 '21

I'm sorry but you are making things up. Taking snippets of code to do little things like creating an array or w.e, is not plagiarism.

Let's say I did a very basic setup for an array, and I'm calling the outputs based on user input.

If someone did the exact same thing for their program, it's possible our code is going to be exactly the same regardless of if we grab it from an open source site or not lol. Is that plagiarism? No.

11

u/SpaceZZ Apr 27 '21

Lol. Ok. But to be on the safe side, I'm gonna contact the best - Harvey Birdman, ok?

0

u/iggy555 Apr 27 '21

Saul Goodman

0

u/[deleted] Apr 27 '21

Harvey would kick Saul's ass.

1

u/JasburyCS Apr 27 '21 edited Apr 28 '21

This isn’t good advice. Nobody will/should be contacting lawyers on an every day basis for stack overflow usage.

There are two main cases. The first is SO examples that are not novel in any way. If I look up “what’s the best practices today for opening a file for reading in C++?” Someone might post a reply saying “Here’s what the official standards document suggests for C++ 17. Use this method.” No citation is necessary.

The second case is when code is novel. This is when the author of the SO comment gives code/algorithms/examples that he/she invented. It contains code that is non-trivial and has a unique solution to a problem outside of those readily available in sources like language docs. In this case, *obey the license. * There’s a default CC license for SO posts on how code can be used/modified/distributed. If the code is hosted elsewhere such as GitHub, other licenses may apply. When in doubt, contact the author.

Source: I’ve taken industry trainings on legal SO usage for a large tech company

2

u/IlliterateJedi Apr 27 '21

I'm honestly shocked that you are being downvoted heavily for this. I was literally reading a pip module a few days ago that cites stackoverflow in the code. It may not be for code snips but it it's not wild to think that someone would do this for code they pulled from SO.

2

u/[deleted] Apr 27 '21

I almost don't believe you. You should site the creators of python every time you use a while loop.

5

u/SMTG_18 Apr 27 '21

When you make your whole discord bot and don’t know any JavaScript. Definitely not me.....

5

u/billsil Apr 27 '21

What's wrong with stack overflow? I've been programming in Python for 15 years. I STILL couldn't tell you how to check to see if a file is binary or not and I couldn't write a unicode safe method to do it. I have a code to do that and it came from StackOverflow.

Rip off all the code you can. Just make an attempt to understand it, get better, and know when it works and when it doesn't.

13

u/dnswblzo Apr 27 '21

When you don't give proper credit. Code on SO falls under this license: https://creativecommons.org/licenses/by-sa/4.0/

3

u/billsil Apr 27 '21

That must be relatively new. People have been ripping of SO for many years. There was no license for a long time.

1

u/awdsns Apr 27 '21

It's been like that for a long time, everybody just ignores it: https://stackoverflow.com/help/licensing

0

u/notarobot1020 Apr 27 '21 edited Apr 27 '21

When I think of plagiarism i think of medium like books or songs were the product is on full display for all to view as their own. But programming is different it’s a combination of lots of building blocks to build a final product. Even if you copy a snippet from the internet or a book it’s unlikely to directly fit your purpose directly you will need to make changes to fit your direct purpose. Once it’s all compiled or running on a server the end user doesn’t see all the blocks inside so have no care to know what’s inside they just want the functionality. I think of taking code snippets as a tool, say like using a hammer . If you build a house you don’t need to thank the hammer company profusely and given them credit for the hammer as such an amazing tool, end of the day you still did all the work. And that’s what stack overflow or any internet page is where people post solutions, it’s about a sharing community to get ideas out there and not have to reinvent the wheel every time this is how we progress as a society, open ideas.

-7

u/coder155ml Apr 27 '21

It’s not plagiarism if people post it

120

u/ThePiGuy0 Apr 27 '21

Absolutely, that's what libraries are for! You package up your code and make it easy to import into other projects. Then give it some good documentation (hopefully :D) and pop it into GitHub and PyPi (python packaging index) so you can pip install it.

5

u/[deleted] Apr 27 '21

Was waiting for the word library.

33

u/dizzymon247 Apr 27 '21

Yes programmers always reuse old code. Documentation is key to ensuring you have an idea how to make old code work with new projects.

33

u/aram535 Apr 27 '21

That's what we do there are levels:

  1. bookmarks to Stackoverflow

  2. notepad snippets (gist.github.com)

  3. IDE based snippets with search

  4. Creating a reusable library of functions

  5. Writing Stackoverflow questions and answering it so google backs them up for you.

86

u/[deleted] Apr 27 '21

You are half correct. As we code new stuff, we will remember we've code this specific function before. We then would spend maybe an hour or 2 frantically searching in our previous projects, copy paste it into our current one, and save ourselves from having to re-type that 5 lines of code in the process.

31

u/Rebound86 Apr 27 '21

this is the way

13

u/[deleted] Apr 27 '21

I'm following you for the rest of your Masterclass lessons.

1

u/altopasto Apr 28 '21

You may spend 2 minutes typing that 5 lines, but you aren't considering the time spent in figuring what to type.

Probably 10 minutes.

12

u/AcousticDan Py3 Apr 27 '21

Yeah, that's what libraries are :)

8

u/hob814 Apr 27 '21

Totally saves alot of time. If you solved it once you dont have to do it again all you need to do is make those minor changes so instead of weeks now it takes you days.

And about where to save it, honestly anywhere you will always have access to. Githib, google drive, on your pc. Just when you finish a project keep it somewhere you might need parts of it later.

8

u/a1brit Apr 27 '21

gist.github.com

Place for little one offs that don't need a whole bunch of superflous extra crud around it.

3

u/mathbbR Apr 27 '21

Seconded! Github gists are the natural place for your matplotlib template

7

u/IvoryJam Apr 27 '21

I save mine in a private repo in github

13

u/pancakeQueue Apr 27 '21

Coding chunks not so much but steps to do stuff and how to do it I store in markdown files.

5

u/VintageReptile Apr 27 '21

Yes. Yes they do.

5

u/macacochato Apr 27 '21

"is the space pope reptilian?" - futurama

4

u/[deleted] Apr 27 '21

All the friggin time

4

u/jenanemone Apr 27 '21

I save my snippets and programs into several folders and add ridiculous amounts of docstrings and notes. This worked at first - then I realized I was working in different machines, and so now I’m using a private GitHub repo, too. I am very much still a beginner, so it will be interesting to see if this holds up when my projects become more complex.

3

u/Odd_Analysis6454 Apr 27 '21

Create two stack overflow accounts. Ask question that this code solves with one account and respond with the code from the other account. Then you google the problem every time and find the stack overflow post.

1

u/GeekSpud Apr 27 '21

Brilliant!

3

u/[deleted] Apr 27 '21

I have entire private repos filled with reference code.

3

u/higgshmozon Apr 27 '21

Yes! The storage of the code though depends on how/where I’m using it.

A lot of people are mentioning libraries and packages. This is absolutely a great way to stash away a lot of regularly used code and make it convenient for reuse for others. It also makes a lot of sense if all the snippets pertain to a specific cohesive project or goal.

You mentioned matplot lib which makes me think some of the cases you have in mind are a bit too specific to find much use in a library? If so, I find myself in this situation regularly, and in these cases rather than writing a library I’ll either just store them on GitHub, in gists, or just in a notepad.

3

u/Dogeek Expert - 3.9.1 Apr 27 '21

Yes, and they are called libraries. Whenever you have a piece of code you want to reuse, you make it into a library.

You can make libraries for all kinds of use cases, and generally, you'll have one use case per library you make. It has several advantages, the main ones being that you will make the library much more robust than what your orignal code did. You'll handle more types of input, write usually better, more efficient code, with a standard coding style.

The second advantage is that it's easy to use, a library, in python requires nothing more than pip install mylibrary to install it (or whatever package manager you want to use, poetry, easy-install, pipenv...), once it's uploaded to the python package index.

2

u/Kessarean Apr 27 '21

definitely

as for where to put it, a private repo on github or gitlab are the usual go-to.

2

u/SpeechKingdom Apr 27 '21

I have a terrible memory so when I am doing new projects I have my old ones open on another tab just so I dont get lost lol (Jupyter notebook stuff)

2

u/PricedPossession Apr 27 '21

I am not that Pro.. so I don't create libraries. But I will tell you how I refer the code that is lying somewhere in my computer. I use "search in files" feature of notepad++ , most of the time I remember some part of the code. Using that I search the files where it was used. It's pretty handy to old codes.

2

u/Jeklah Apr 27 '21

I am not sure if this is common practice, but it is most definately something I do.

I haven't organised it into a repo, I just have a txt file I keep open in sublime text that's saved on dropbox...I probably should make a repo.

2

u/william_lidberg Apr 27 '21

I think he is on to us...

2

u/slimmsady Apr 27 '21

* get stuck on an issue

* remind myself repeatedly to save the solution when i find it

* Finds the solution after 76 hours

* Copy pastes it into my code and forgets about it.

* Rinse and repeat

2

u/DiogoBerti88 Apr 27 '21

If i Know i'll be the only one using, i usually create a gist for the snippet. But If you think It Will be good for others, try to publish It as a lib

2

u/[deleted] Apr 27 '21

TIL about gist, and I feel stupid for not hearing about it before. Are there other useful stuff that I'd feel stupid for not hearing about it?

1

u/Nomikos Apr 27 '21

and I feel stupid for not hearing about it before

Well don't :-p As for other useful stuff - like, probably? I bet you know useful things I never heard of, too.

2

u/TheRainbowCock Apr 27 '21

Im gonna be honest I try to look at my own solutions as much as I can so I dont regoogle "how to get value from object" for the 20th time after not needing to implement that function in the last few upgrades.

2

u/synthphreak Apr 27 '21

Yes, it's called a package.

Basically if you have your sweet template, just encapsulate it as a function that takes the variable components as arguments. If you save that function as a script, you can import it into other scripts and use it just like you would any other function.

This is part of the process of keeping your code DRY, a super important design principle for good clean code.

2

u/[deleted] Apr 27 '21

Yes ! All the time actually, this is why I upload all my code to GitHub, so I can access it from anywhere and just simply look at how I coded something.

2

u/mon0theist Aspiring Pythonisto Apr 27 '21

That's basically what libraries are

2

u/Vortetty Apr 27 '21

This is called a library

2

u/KingJoia Apr 27 '21

That is why I comment every piece of code I make. Just to put that into folders after folders that I spend 3 hours looking through just to find a function that orders a string

2

u/Houdinii1984 Apr 27 '21

I just started using a tool, 3cols, yesterday. It's only purpose is to hold and organize code snippets and so far it looks like it does the job well. (https://www.3cols.com) Basically you separate your snippets into categories (like languages) and subcategories (like basics, arrays, objects, etc). Not a lot of bells and whistles, but I learned a long time ago that with snippets you only need searching and filtering and the rest is just overcomplicated.

Also, most IDEs have some way to reuse code built in. I personally use JetBrains Webstorm and PyCharm, and both use what's called live templates. I can type in a phrase, like "expanded_switch" and hit tab and my saved code for a large switch case statement will type out on the screen which I had saved earlier. Each IDE has a different way of achieving this, and most use a tool called "Emmet" ("Emmet tutorials" leads to a lot of documentation on the subject) which has its own set of snippets built-in as well.

1

u/SergiHo Jan 27 '22

I LOVE 3Cols. It is by far the best snippet manager I have found. 👍🏼

I just wish they would release PLUGINS for JetBrains and VSCode!
If they did, their exposure would SKYROCKET.

3

u/MysteriousK69420 Apr 27 '21

I always spend hours trying to find the file named X in the sub folder Y of the folder Z. :c

2

u/wobblycloud Apr 27 '21

In Pycharm there's something called Scratches, I use it keep handy scripts for reuse.

1

u/Quant3point5 Apr 27 '21

Constantly

1

u/ViralLola Apr 27 '21

Yes, good code is adaptable and reuseable. I put mine on Github and Repl.it.

1

u/Juhani124 Apr 27 '21

I havent done this on python coding yet but when i was learning cad cam coding we borrowed alot from other works... Saved alot of time

1

u/BlackSmithOP Apr 27 '21

If it's a small people of code that makes something much easier to do, I just make a gist for it with a basic description. I've found a non insignificant amount of code from gists that gave me an idea of how to do x when the docs weren't enough.

1

u/[deleted] Apr 27 '21

Of their own and useful code of others, basically a program is a puzzle.

1

u/[deleted] Apr 27 '21

I guess that's what functions and packages are for

1

u/Ardit-Sulce Apr 27 '21

Yes, but if it's spaghetti code it's going to be messy. If it's ravioli (i.e., you have used classes) it's easy to pick individual pieces from the plate.

1

u/husudosu Apr 27 '21

You don't have to reinvent the wheel every time. It's very time consuming. When you have a good boilerplate project for Flask and for frontend framework like Vue.js with full user management, it helps lot and you can start build your business logic almost immediately. I'm also storing code snippets on my private gitlab server well organized.

1

u/TakeOffYourMask Apr 27 '21

Oh gosh yes.

1

u/CrimsonBolt33 Apr 27 '21

If you type something once to fit the program that's normal.

If you type it again for another program...that's forgivable.

If you type essentially the same thing 3, 4, 5+ times you are just wasting time at that point.

You obviously need to be careful with copy paste errors and make sure everything plays nice when you do that but it's generally much faster and much better than constantly typing things.

Coding is not about typing...coding is about solving a bunch of puzzles to make a program. This is exactly why many training programs for kids are literally plug and play code snippets with no typing, because typing out the code isn't as important as knowing how it all works.

1

u/FoxchildWasTaken Apr 27 '21

Just to give a different answer: most of the time.

1

u/[deleted] Apr 27 '21

That's kind of the point of programming in my opinion. I'm not a professional however, this is just what I do as a hobby. I know some people put things they think might be useful in a data base, I started doing it too and I kind of like it.

1

u/BloodyWashCloth Apr 27 '21

Nah man you want that code to be 2nd nature when those problems come up

1

u/[deleted] Apr 27 '21

Yeah

1

u/JunkBondJunkie Apr 27 '21

yes. One time my dad had a problem I solved like 5 years prior at his work and asked for the code.

1

u/[deleted] Apr 27 '21

Good ones do.

1

u/Kriss3d Apr 27 '21

You can always use something like github for private projects. Personally Id just use my own server for that kind of thing. But absolutely we reuse code.

One simple type of code youd reuse alot is create a database, write to it and search in it. No need to reinvent that all the time.

1

u/dearAbby001 Apr 27 '21

Yes. Which is why proper external and internal documentation is key. This way even 10 years later you can come back to your code and update or reuse as needed.

1

u/rpo5015 Apr 27 '21

We have an internal library at work that we created just for this purpose like sorting, getting unique values etc. another way I do this is by creating GISTs on github. I think that’s an excellent way to save snippets of code and also be able to share it as a POC

1

u/Zeroflops Apr 27 '21

Yes, there are two types of code snippets that I keep. The first is small bits of tricks that really don’t make up enough to ‘do anything ‘ of significance. As a simple example might be how to use ZIP. I store those in a folder of jupyter notebooks that are broken up by subject. The notebooks allow me to document the trick and write something that executes it as an example.

The second snippet might be more of a function. I have a library that I store those functions in with doc strings etc. so I can import that library and use those functions. It’s not something I would post because it’s just a bunch of random useful to me functions. An example of this is I wrote a function to build boxplots with Bokeh. Boxplots are not a default option in bokeh, but they are easy to produce by piecing together glyphs. Now anytime I want a boxplots I just import that portion of the library and I’m done.

1

u/bmcle071 Apr 27 '21

Depends on the quantity of code I guess. Im working on a project at work and I have a LOT of code I want to use on the front and backend. So i created a package for it and published it to a private npm repository for re-use across multiple projects.

1

u/relativistictrain 🐍 since 2010 Apr 27 '21

I save StackExchange & Git repositories to my Zotero library, and I have a models repository with some templates for some type of scripts.

1

u/malwareC Apr 27 '21

Of course. If you solved one problem from scratch, you have the knowledge how it works and what's it based on. So there's really no need to do it again and again if needed in future. It let's you visit your work once in a while. Also in future if you learn some new approach the problem it becomes easier to compare and implement at both complexity calculation and ease of use to find out the efficient choice out of them.

1

u/wildpantz Apr 27 '21

I would usually save stuff into my "user" folder I always carried around on my USB and had it also on the PC. Then I bought a new PC and figured there's no need to do that yet, at least for a couple of months, but of course the SSD died and I lost ~5 years of Python related stuff. Now I save on google drive and I'm not stupid anymore. And yes, these "chunks" usually need a bit of restructuring, but restructuring is often faster than building stuff from scratch.

1

u/Fransiscu Apr 27 '21

I have a file with various pieces of code for every programming language I used that I refer to if needed

I also have a Java utility class that I often just include in whatever project I do since it has some useful methods that I always end up using

1

u/skellious Apr 27 '21

yes, make a library and use github to store and track it.

1

u/YodaCodar Apr 27 '21

yeah thats what git is about :D

1

u/[deleted] Apr 27 '21

Put it up on GitHub for your own personal use, make it a package/module for ease of use.

1

u/vorticalbox Apr 27 '21

totally, I started our companies internal SDK because of the amount of reused code all over the place

1

u/expressly_ephemeral Apr 27 '21

No. This is forbidden. \s

1

u/Windows_XP2 Shitty Programmer Apr 27 '21

Basically, I reference my previous projects all the time.

1

u/Informal_Swordfish89 Apr 27 '21

Yes.

But even better, make it a modular library that you can just import next time.

1

u/[deleted] Apr 27 '21

Yes, in my team, we created an in-house package for commonly used functions. We also created uitls.py for repeated function usage.

1

u/Se7enLC Apr 27 '21

Not specifically - but my code is all in git somewhere, so it's a fairly regular occurrence for me to grep through old code looking for how i did it last time.

If it was something I was really using a LOT, I would probably write it into a library, though. More likely it changes each time and I learn something that can be done better with each iteration, so it's good to just have them as starting points.

1

u/epopt Apr 27 '21

Saved in txt files in a special directory. I grep looking for appropriate snippets. I still have some perl 4 junk in there...

1

u/menge101 import functools Apr 27 '21

I had an amazing figure template on matplotlib that I might want to use again is there a repository where you should put these things ?

All the things you install via a requirements.txt file are these things.

1

u/[deleted] Apr 27 '21

That's pretty much what Python is. Saved bits of C/C++ code.

1

u/[deleted] Apr 27 '21

Often times I just dig up old programs I wrote that have what I want. I probably should have a repo for that kind of stuff tho

1

u/vodoko1 Apr 27 '21

I only save my programs if I want to use them for something else.

1

u/neiliodabomb Apr 27 '21

Whenever you import a module, you are using a prewritten chunk of code

1

u/swapripper Apr 27 '21

Take some time to create snippets in VS Code for repeated code( with optional arguments). Will save you a ton of time in the future.

1

u/SolitaryVictor Apr 27 '21

Before? Yes, absolutely. That, basically, was your "Experience", is the amount of proof you can solve some shit. People coded for github just for the "portfolio" trying to show of as much as possible.

Now? Probably not. You just dump them on stackoverflow and since we all do it eventually you just google the problem and find a solution.

1

u/Berkyjay Apr 27 '21

I literally have a git repo that stores scratch code.

1

u/Decency Apr 27 '21

I occasionally reference existing code for some pattern I've needed before (for example: creating and launching an external process) but don't specifically make an effort to set these things aside. They're easy enough to find when I need them with good search/git faculties.

When I'm learning a new language I do exactly this to create essentially a translation sheet from x to python, though!

1

u/SlaimeLannister Apr 27 '21

Yes, it’s called importing modules

1

u/Far_Inflation_8799 Apr 27 '21

Code is reusable and you can build libraries with templates and many times you import these chunks of code when you want to use then in your apps.!

1

u/[deleted] Apr 27 '21

Either that or throw it into a library

1

u/RobinsonDickinson Apr 27 '21

use github gists or make a repo to save your reusable code.

1

u/[deleted] Apr 27 '21

I used to think that having to check back projects to recall how I did something was a sign that I haven't internalized it, but sometimes you know exactly what to do conceptually but you miss one tiny detail (either something smart code-wise or a library you imported) that isn't worth wasting time trying to recall it and you're better off reference your old work instead.

1

u/flash_309 Apr 27 '21

I do save some code snippets only from the areas, I feel are difficult for me. But don't make a habit of saving everything you see. Most of the time if I had tackled the problem before I can do it again. So don't stress over it. Even if things go south later, you can always head back to stackoverflow or reddit.

1

u/brews Apr 27 '21

gist.github.com

1

u/[deleted] Apr 27 '21

I am supposed to, but in the end I bookmarked the stack exchange post.

1

u/[deleted] Apr 27 '21

Programming often is about creating good high quality work quickly and at a reasonable cost. If you do not save, borrow and reuse work, will (probably) never get to the next level.

(This is probably a borrowed quote. Feel free to add it to your own library of borrowed quotes)

1

u/itsmitchy_b Apr 27 '21

Guilty. If it worked before then it can work now. When I copy and paste, then make an improvement, I’ll update the code I got it from.

1

u/[deleted] Apr 28 '21

I usually save my code on Github and separate .py files. Since I'm a data analyst by trade and frequency checks for categorical variables is pretty much mandatory for just about everything I wrote my own rudimentary frequency function that I import at the start of every notebook file.

Functions that are saved on separate .py files are imported the same way as you would import modules.

from frequency_func import *

The only thing you have to note is that when importing a function like this there might be module import errors caused by the .py file not containing import statements of its own. To circumvent this simply add a few import statements into the .py file.

1

u/thedjotaku djotaku Apr 27 '21

> is there a repository where you should put these things?

You have a few options here. If it's highly specific to you, just put them in a github repo or two. One of my oldest github repos is a collection of bash utilities I'd made or found. https://github.com/djotaku/Bash-Utilities

IF you think it'll be useful to others, then see if there's a repo for your programming language like CPAN, PyPi, NPM, etc and put it there. This utility I made for btrfs snapshots is useful to anyone else using btrfs, so I put it on pypi: https://github.com/djotaku/Snap-in-Time can be found at https://pypi.org/project/snapintime/

0

u/CatOfGrey Apr 27 '21

Yes.

I'm in a Master's Degree program in Data Analytics. One of the objectives is to have code that I would use as a 'tool kit', and all I would need to do is change the one line that opens the new data file, and the lines that have some specific steps that have to do with that one particular file.

Then, the 100 things that I need to do with the data (do some computations, print a graph, identify missing data or weird data....) is all the same.

-7

u/amykamala Apr 27 '21

Yes, it’s called object oriented programming.