Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Python code examples w/ 500+ lines


  • Please log in to reply
4 replies to this topic

#1 ozzynotwood

ozzynotwood

  • Members
  • 53 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:02:38 AM

Posted 11 November 2017 - 08:13 PM

Hi everyone, I'm learning Python 3 and I want to try a learning technique that I saw someone using. This technique will be added.to my study routine, not replace.

 

I want to print out an existing program and type it back in, that's it, it would just be something extra to do on a Sunday morning, if it makes no difference then no loss. What I do need is existing code. I'm having trouble finding completed code with 500+ lines.

 

Can anybody direct me to a site where I might find something suitable for this task?

 

Thanks!

 

JC


Z370 AORUS Ultra Gaming 2.0 (TPM 2.0), i7 8700K, 32gb HyperX Fury, H100i CPU Cooler, GTX1050 Ti WindforceBenQ GL2580(H) x3, 970 PRO SSD, WD Red HDD x3, Seasonic Focus Gold 650w, Corsair Obsidian 750D Airflow, W10 Pro.


BC AdBot (Login to Remove)

 


#2 KingDavidlll

KingDavidlll

  • Members
  • 297 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:01:38 AM

Posted 11 November 2017 - 08:24 PM

Searching for what you want in a program and putting github in the search as well will provide you with much better results rather than example program. Simple examples of python scripts with a large variety of keywords can easily be found in simple games in my opinion. The link won't have 500 lines in one file however it will have over 500 lines in all of the files, and teach you how programs should be structured (although I haven't necessarily looked at the code to see if it's well written).

https://github.com/aqisnotliquid/python_rpg

I honestly think it's better to be given a task and use example code and modify it to be a better use of time rather than just copying like for like.

#3 magiclcj

magiclcj

  • Members
  • 5 posts
  • OFFLINE
  •  
  • Local time:10:38 AM

Posted 17 April 2018 - 08:45 AM

KingDavidIII probably has the best idea.

 

Al Sweigart has written a few book about learning with python. https://inventwithpython.com/ Most are free online. Each book has a theme for certain programs. They won't be 500  lines, but each program has a theme which will help you recognize why parts of the code are there.



#4 Acecool

Acecool

  • Members
  • 2 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:11:38 AM

Posted 28 April 2018 - 11:58 PM

While repetition isn't a bad idea, blind repetition isn't good because you may inadvertently pick up some of the authors original bad habits ( if they exist ) and they don't reinforce certain ideas and may cause issues with other languages.. I'd recommend working a problem based on your level of experience from scratch, or partial - solve a problem using the language or create something new... 

 

Some things which exist in Python but not other languages: Variable Scope - now, it does exist in Python but it is incredibly lax - for instance if you create a function and create the variable in an if statement you can use the same variable outside of the if statement - but in every other language I work with ( dozens ) you have to initialize or declare the variable ahead of time otherwise anything outside of the if statement won't know what it is...

 

The whole idea of forced tabbing - while this is good and can help build up a coding standard for you to apply to your work for other languages, and it forces you to make the code somewhat human readable, it doesn't take it all the way... It is fantastic for the language because Python is powerful and its high level ( meaning easier to learn, use, etc. and the back end does a lot for you. ) and using tabs to designate scope or possession means less characters typed to designate scope or possession meaning much faster turn around when converting your program from the design stage to code stage... The one issue I have is many people use spaces instead of tabs which cause grossly inflated file size... To give an example I converted spaces to tabs in a file which was around 800KB in size and it took it down to 200KB... That's HUGE... It's also why I will always use tabs instead of spaces...

 

 

 

Some things I recommend when teaching - and these are useful all around: When you code something, any time you repeat code, turn it into a helper function... Give it an appropriate / easy name and then use it instead of the repetition everywhere...

 

When using a language, it isn't a bad idea to rewrite a function if it makes sense - give it a new name to retain backwards compatibility if others use your library with something else... For instance, there are draw functions which use startx, starty, endx, endy instead of simply x, y, w, h - meaning when you go to draw multiple objects ( say it is a rectangle and you want a border, using x, y, w, and h you can offset the second drawn [ which is drawn above the background / first ] by the border size in x and y, and subtract border * 2 from w and h ] - things are sometimes easier using x, y, w, h instead of start and end because the math becomes much shorter when you call the helper functions rather than the originals using start / end x / y vars )... It may not be the best case all the time, but for certain tasks it makes sense....

 

Create a coding standard and stick with it... A coding standard is something used by companies in the real world to ensure their code looks like it was written by a single person or by a thousand people all using an identical method... A coding standard is how you style your code... Ie I use airy declarations so instead of func(x,y,w,h): I use func( w, h, x, y ):... Additionally, I use _s to designate local, __ to designate private / internal ( which works with Python ) and none for globals... I use all upper-case variable names to designate constants and enumeration variables. I also prefer underscores in variables to separate words instead of UpperCamelCase or lowerCamelCase... All functions have 3 comment lines above them, unless single-lined then 1, with 2 empty lines above them, unless single lined then 1... The center line has a description of the function and I am leaning towards adding argument / return descriptors to automatically generate documentation... All classes have 3 comment lines above them with the center line having the description... All variables of a singular data-type have FIXED names... so _p for a Player object, _v for Vehicle, _w for Weapon, _data for a data Table, and many more ( This example is from my GMod Lua Framework but it applies to everything applicable - I use set variable names along with underscores because if I ever have an error come up then from first glance I can immediately see the scope which reduces debugging time by a LOT and the data-type supposed to be contained with the variable, etc... ) - so func( x, y, w, h ): actually looks like func( _x, _y, _w, _h ): -- Note: Python uses double-underscores for something very specific - for my purposes it doesn't affect anything adversely but make sure your coding standard works for the languages you use - It's inconvenient and costs a lot of time and money when you need a new coding standard for EVERY language especially if they are completely different from the previous - so try to stick with one method and adapt as much as you can to other languages when it isn't possible to keep the identical variant...

 

If you use Sublime Text - Install my addon by installing Package Control, adding this repo: https://bitbucket.org/Acecool/AcecoolCodeMappingSystem and then installing this package: AcecoolCodeMappingSystem or Acecool Code Mapping System... Using the context menu you can open the panel... It'll map the language to give you a short / simple overview of the entire file ( whether 500 lines or 10000 ) in as few lines as possible such as giving you class headers, function declarations, commented Note:s, Task:s, etc.. and more all while organizing the data into categories alphabetically ( you can sort them by line asc or desc or alphanumerically asc or desc )

 

It is pre version 1.0.0 so not all of the features exist - I am working on optimizations and giving it a toolbox while improving the output so that every character can be clicked on / hovered for data... Right now double-clicking lines with :xxx at the end will jump you to the line in the file... Soon, that won't be a requirement with my region mapping system so bookmarks, macros, snippets, etc.. can be triggered along with links, or tooltips to appear on hover, and more. The toolbox feature will give you a live search filter / mask for the entire output data to act like a dropdown in IDEs for choosing classes to show / hide, etc... 

 

It can also help identify typos, and syntax errors ( basic functionality has been added but soon a rule system will be integrated and work in conjunction with a lexer to provide more accurate results )...

 

You can create your own mappers too - so if you create a class or function which creates AccessorFunctions and you want them to be expanded into the functions they create ( see lua.py to see an example with GMod Lua and AcecoolLua_Framework for examples on this - soon will be Lua.py, GMod_Lua.py, etc... ) but there are many callbacks which can be used to map or change or scan the data to process it how you want... I am going to alter the callbacks soon though - instead of all of the args I'll be using an object as an argument to make it future-proof because of all of the features I'm adding and editing a ton of files every update isn't ideal.... a good reason for a design phase to exist - unfortunately I made a mod for another Sublime Text addon and the author said that's what he wanted his to be and then didn't want to integrate any of the features ( it took anywhere from 10 to 100 or more lines to add a single item to the map, with no category system if it found something in a 'category' it'd output it, then another, then the first but instead of combining the data you'd see 3 categories instead of 2.... which is why I made this system back-end first but it also had the issue of circular imports because of how he looked for files and wouldn't give me an alternative so I'm making it stand-alone and while it is taking longer than it would've otherwise I'm better off because it means more features, and more control )....



#5 PerspectiveObjective

PerspectiveObjective

  • Members
  • 457 posts
  • OFFLINE
  •  
  • Gender:Female
  • Local time:10:38 AM

Posted 14 June 2018 - 09:27 PM

Regarding """""  Learning Technique " ozzynotwood

Quickly:::: just an fyi that profs somehow forget to mention.

Some IDE's , coding programs and text editor toys don't know or handle input() depending on O/S and version of Python, all say oh we have this fix that generally never works however that statement isn't pasted in large text on the front of the IDE/text editor kid's website, you get to find that out and pull your hair later, so satisfying. Enjoy!


Edited by PerspectiveObjective, 14 June 2018 - 10:16 PM.

Oh those are really nice, where`d you get them done at? YOUR NAILS SILLY! Banter/Wit is a primary member requirement to colossal project solution.  Not to toot my horn......          (                (        (       (     :trumpet: but, who else will!? teeehehehehehheeee!~~~8 : P Additionally::: "Do, or do not. There is no try." - Yoga





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users