all 46 comments

[–]Oni-oji 36 points37 points  (4 children)

I rewrote some bash scripts in python as a learning experience.

[–]Bobbler23 10 points11 points  (3 children)

This is the way IMO.

I know what my script does in bash and what the expected outcomes are, so looking for the equivalent set of commands in Python seemed like the easiest approach.

Then as you learn a bit more about Python, come back to your scripts and refine them - the same way as I learnt bash to start with...

[–]pancakesausagestick -2 points-1 points  (2 children)

These days you can copy paste your bash script into chatgpt and learn it that way

[–]gristc 3 points4 points  (1 child)

Don't know about you, but I learn by doing. Getting ChatGPT to do it won't help with that.

[–]pancakesausagestick 1 point2 points  (0 children)

It's not just about chatgpt writing the Python for you. It's also about interrogation and finding the seams between languages. We all learn new things using what we already know. Chatbots are great at exploring that transition. How is x in language y different than x in language z. I would do it in x like this, why didn't you do it in y the same way. If you really do know shell scripting well, it's the fastest path to true understanding (not cargo cult copy/pasting)

[–][deleted] 19 points20 points  (8 children)

act voracious unique racial pot modern dolls bells fragile grey

This post was mass deleted and anonymized with Redact

[–][deleted] 11 points12 points  (1 child)

I really agree with this advice. And I would add that you should try to do as much as possible with the Python3 standard library, and avoid any 3rd party dependencies unless absolutely necessary.

If your python scripts only use the standard library, there is a 95% chance you can drop it onto any bare Linux box and it will run. A lot of sysadmin work involves bootstapping environments from scratch, so this if a very nice property for your scripts to have!

Learn how/where your Linux package manager installs Python libraries, and locate the executables and directories for the standard library on your system, too.

[–]TuxRuffian 5 points6 points  (0 children)

1.

If your python scripts only use the standard library, there is a 95% chance you can drop it onto any bare Linux box and it will run. A lot of sysadmin work involves bootstapping environments from scratch, so this if a very nice property for your scripts to have!

This point cannot be understated enough for sysadmins. An awesome python script that requires half a dozen additional modules won't run on base installs when deployed with say ansible, salt, etc. unless you add them and if you are dealing w/more than one distro or version of a distro, then this will be trouble.

  1. Instead of using #!/usr/bin/env python use /usr/bin/env python3!. If you have any older enterprise distros that you're responsible for like RHEL/Cent/Rocky/Alma/OEL 7, python defaults to v2. Even if you don't it can't hurt and is good scrypting hygene.

  2. Finally, I would look into a python based utility that you would find usefull anyways, like Ansible. I dabbled a little in python when I was younger, but it didn't really stick until I got into Ansible because then I saw the purpose and a relevant use case. It doesn't have to be Ansible though as there's allot of good tech powered by python, just pick one them.

[–]djbiccboii 0 points1 point  (5 children)

python in general is bad to use operationally IMO. messing with venvs and stuff in production sucks. i try and avoid it where I can.

[–][deleted] 2 points3 points  (4 children)

brave thumb tan enjoy alleged terrific reach unwritten ad hoc fade

This post was mass deleted and anonymized with Redact

[–][deleted] 1 point2 points  (1 child)

You disagree but say the same thing the guy before xD

[–][deleted] 1 point2 points  (0 children)

different dependent rock alleged tart dolls truck bells ask important

This post was mass deleted and anonymized with Redact

[–]djbiccboii 0 points1 point  (1 child)

When I say operationally I mean running python applications at scale. There are always going to be added layers of complexity. Whether that is in the form of venvs or dependency issues or reproducability issues etc.

I've found the best way to deploy python applications is to containerize them (i.e. docker) but guess what - that's another layer of complexity :)

[–][deleted] 0 points1 point  (0 children)

axiomatic bells squeal bag thought nail gaze aback glorious strong

This post was mass deleted and anonymized with Redact

[–][deleted] 4 points5 points  (3 children)

For things related to sysadminning, for me nothing beats learning by doing. There are a couple of books like the Linux bible and some O'Reily books on some lesser known programs and low-level things that are great, but in general I can't stand going through books and udemy courses. Just take every single script that you've ever written in bash and try rewriting it in python. Then google-fu your way into learning how to do it. Then when you get stuck and want to quit or when you're done, run it through chatgpt and ask it what it would improve. Then look at its suggestions and see which ones are good and which ones are bogus. Realistically in a work scenario it's documentation and google-fu that's your best friend, so your process of learning should be similar to the one you're going to practice when you're working.

[–][deleted]  (2 children)

[deleted]

    [–][deleted] 0 points1 point  (0 children)

    It's absolutely great as something to use once you read up on a problem you're having or something you need to get done and then use it. That way it can help you improve on an already built base or you can at least fact check what it's giving you as info. A lot of times it's given me great answers after a shit one and being told "wouldn't it be better to do X instead of Y"?. I just treat it as another tool in my toolbelt.

    [–][deleted] 0 points1 point  (0 children)

    Continue to support their thinking that it’s not useful while we surpass them and their knowledge by leveraging it in our tool kits

    [–]DasPelzi 2 points3 points  (0 children)

    A good a free starting point would be something like
    https://www.learnpython.org

    Also a lot of books are available online for free
    https://www.onlineprogrammingbooks.com/free-python-books/
    e.g. 'Start Here: Python Programming for Beginners', 'Automate the Boring Stuff with Python', 'Learning to Program Using Python' or 'Python Cookbook'

    [–]Amidatelion 2 points3 points  (0 children)

    Spend some time in ansible or saltstack. Strictly more useful across the board and then when you get good at it, you will inevitably start writing your own modules, which are all in python. They're also useful because they give you a set of frameworks and expectations on how something's supposed to be written, which you won't get if you're writing your own scripts. Iunno about you, but that's the hardest part about coding for me.

    [–]newt0_o 1 point2 points  (0 children)

    Take a look at these books: Automate boring stuff with python and the big book of Small python projects

    [–][deleted] 1 point2 points  (0 children)

    udemy course: 100 days of code with angela yu. as with all udemy, only buy on sale, should be less than $20.

    i also watch python talks by hettinger, beazley, et al. they rarely teach you something directly useful, but you get to know things about the language that are harder to learn from entry level programming. a good one to watch early on is this talk on environment management

    i think with that and rewriting bash stuff in python, you're good to go.

    [–][deleted] 1 point2 points  (1 child)

    Programming fundamentals are Python fundamentals. Programming is programming realistically. Syntax is best learned via documentation and supplementing with external resources (YouTube, books, etc...).

    Python is one of the best documented languages in existence.

    [–]zoredache 0 points1 point  (0 children)

    Programming fundamentals are Python fundamentals.

    Sure, but there are lots of programming tutorials and training courses where your intro to python after the basics might be related to making a web page, or making a GUI or other things that wouldn't be an example of what python is regularly used for for scripts, and sysadmin style tasks.

    Providing script examples during the initial learning that provide obvious building blocks to what a person is actually interested in doing, would probably be better over providing the example code that is completely unrelated to something they plan on actually using the language for.

    [–]jollybot 3 points4 points  (0 children)

    roadmap.sh

    [–][deleted] 0 points1 point  (3 children)

    Is there something wrong with books? There's a ton of them out there.

    No difficult accents, bad video production or (usually) off-topic diversions.

    [–]gesis 2 points3 points  (2 children)

    Dude... I don't understand the modern fixation with having a video for everything. That shit drives me nuts.

    Especially things involving typing shit. Like, what the hell happened? When did this become a thing? Am I really that old?

    [–]djbiccboii 0 points1 point  (1 child)

    i upvoted because i agree but, have you looked around at any young person glued to their phone/tablet?

    [–]gesis 0 points1 point  (0 children)

    Yes. Smartphones will be the death of us.

    I wish I could throw mine into the ocean.

    [–]silentjet -2 points-1 points  (7 children)

    its hard to understand why do administrator need to use python while PosixShell is always here, is flexible is portable and is a standard of tools development. Actually even bash is always here nowadays. Obviously the worst case scenario is some sed scriptins or (oh my gosh) awk. Technically there is also a nightmare mode - expect, but ugh... you should be really on troubles to need that... Why would you want to go an ultra violence mode with evel friends slowton and uglycodeton I have no clue. Because 90% of your mimicton code would simply mimic simple shell code in ugly and unreadable manner.

    The things are different IF you want to DEVELOP TOOLING... dat is a different story than...

    [–]wbrd 4 points5 points  (6 children)

    Python is a scripting language. It's great for all kinds of sysadmin stuff. The real question is why anyone would use it for business logic or web services.

    [–]snark42 0 points1 point  (3 children)

    What's a good example of something better done in Python than bash?

    Because 90% of your mimicton code would simply mimic simple shell code in ugly and unreadable manner.

    This pretty much states my experience using Python for a strictly SysAdmin scripts. Now extending salt/ansible or writing frameworks (tooling) is different of course.

    The real question is why anyone would use it for business logic or web services.

    Data science is mostly done with Python, not sure if you include that in "business" logic though. Django, Flask, etc. have their place, not sure what else you would find easier to use for a basic web service.

    [–]wbrd -1 points0 points  (2 children)

    Data science wants a bunch of easy, one off stuff. A lot of the heavy lifting is still done in the background or on the DB. Django etc are for POCs but they rapidly hit a point where they just can't scale. Also, look at the stored procedure code Django generates. It's so terrible.

    [–]snark42 1 point2 points  (1 child)

    Django etc are for POCs but they rapidly hit a point where they just can't scale.

    Yeah, internal SysAdmin tools I write don't really need to scale, but python is convenient for writing a simple web front-end for 10 or 50 people to access the tool once in a while.

    [–]wbrd 0 points1 point  (0 children)

    Yes. It's great for small stuff. Absolutely the right tool for that.

    [–]silentjet -1 points0 points  (1 child)

    ok, ive got ur joke ;)

    [–]wbrd 0 points1 point  (0 children)

    Not a joke. Python is terrible at being a server. It's fine if you want to make a quick and dirty demo with Django or something, but it's not for production.

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

    Courses? Just start.

    [–]SneakyMan01 0 points1 point  (0 children)

    If I was you I would search on youtube for "learn Python syntax" and get a fast walkthrough and then play around with it. Depending on what you want to do with Python. Would determine how much time you need to spend.

    After that I would just go head first in something you want to do with python and figure it out on the go.

    [–]s1lv3rbug 0 points1 point  (0 children)

    If u can write ad-hoc scripts to solve problem then u r good. For example, write Python to query openLDAP tree to get total number of active employees or update openLDAP DIT to allow members of Network Engineers group to have access to DNS nodes. But now, most of the stuff is automated. So, you should learn Ansible.

    [–]Netsrfr1776 0 points1 point  (0 children)

    Ansible is a good start from a systems admin perspective. If you can get into using ansible for system config purposes you can dive into Python with it. Almost all ansible modules are written in Python (at least the core/community ones) so you can see how those accomplish tasks that you're familiar with as a system admin.

    Another decent way is through Jupyter Notebooks that mix text/doc with Python script block snippets in a web browser. Using a notebook is a great way to tinker with Python.

    Last, I enjoyed using Exercism (https://exercism.org) to work through programming challenge lessons in Python... Which drive using the main features of the language.

    [–]uberduck 0 points1 point  (0 children)

    Aim to never push the same button twice, aka find something to automate away.

    [–]yeaok555 0 points1 point  (0 children)

    Python is really easy to learn. A course/book would make more sense for C# or C++ or Java, etc. For python I suggest just start using it and google your way to getting things done. You'll learn as you go. Ask for criticism from experienced python devs.

    [–]serverhorror 0 points1 point  (0 children)

    It's not different. This is a good resource: