• Programming,  Python

    Python script to identify CIDR notation

    It’s not uncommon to expect CIDR notation vs an IP address in a script (e.g. one that’s being passed to another API. So the following script is an example of using the re module to identify if an entry is in CIDR or not (since it’s not always obvious how to go about doing so): The array can easily be changed or filled with a network mask or array of them rather than fixed as they are here – and the output could easily be to call another function rather than to just print output.

  • Programming

    Google Cloud Function to convert YAML to a plist

    TLDR: The GCF at https://github.com/krypted/tinyconverters/blob/main/GCF_YAML_to_PLIST.py will convert basic YAML to PLISTs. Here’s a Google Cloud Function that converts YAML to a property list. The function takes the YAML data as an event object, uses the yaml and plistlib modules to convert the YAML data to a property list, and returns the property list. To deploy this function, use the “Create Function” button in the Google Cloud Console. When prompted, select the Python 3.7 runtime and paste in the following script: Once the function is deployed, call it with a POST request to the function’s URL and include the YAML data. For example: Given the above input, the response body will…

  • Programming

    Lambda Function To Calc The Fields In A JSON Document

    Below is a Lambda function to return the number of fields in a JSON document. This Lambda function expects the JSON document in a body field of the event. It loads the JSON document using the standard json.loads() and counts the number of fields by retrieving the keys of the loaded JSON object and calculating the length. The expected response would be a 200 status code if successful, with the number of fields as a string in the response body (which can easily be changed to an int, but made sense to leave as str so if it got some wacky data I’d see that). If the JSON document is…

  • Programming,  Python

    Google Cloud Function To Add JSON To A Cloud SQL Database

    Here’s a Google Cloud Function that takes some arbitrary json (in json_data) and posts it to a new record in a Cloud SQL database. No error handling or deduplication/matching, just a straight post: https://github.com/krypted/tinyconverters/blob/548892cc377e1063770ab4a8cd53dc6573bae950/json_to_cloud_SQL.py Before using, it needs information to connect to the database, so customize the INSTANCE_NAME, DATABASE_NAME, and TABLE_NAME for the INSERT. For more on Cloud SQL, see https://cloud.google.com/sql/docs.

  • Python

    Lightweight Audio Transcription Script

    Quick and dirty audio file transcription script that should be easy to use as a droplet or for desktop transcribing automations at https://github.com/krypted/lightweighttranscription. To use it: Install awsCLI and boto3: pip install boto3 –user (or pip3 install boto3 –user) Mac client available at: https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-mac.html Run the $ aws configure setup Set access key, secret key, region and output format Instead of passing the credentials in the script, it is recommanded to install awscli and setup credential profile. If running as a microservice, simply hit the endpoint in the script instead. If running on a computer, keep the Input file and script in the same dir to avoid search path failures. Run script `python3 …

  • Machine Learning And Artificial Intelligence

    Generic Machine Learning Recommender Script

    Been working more on building really generic and simple machine learning tools. This one is a generic recommendation script built to run as a lambda or gcf. This iteration on my GitHub is built to run locally but it’s straight forward enough to import json, parse, and run it as a microservice. Requirements numpy gensim nltk==3.4.5 textblob==0.15.3 Usage Run locally, the recommender crawls through a column of a csv and matches the recommendations for similar content. Those are based on the content passed in the –text field. Can use the –recs option to define the number of recs you’d like to recieve in response. python recommender.py --file='my.csv' --text="Flash update" --column="title"…

  • Python

    7 Python Machine Learning Modules To Get Acquainted With

    A lot of machine learning work has been done in python. Therefore, there are a few python modules that make the work of normalizing data, analyzing data, and training algorithms much easier. Let’s look at a few of these and if you have suggestions, please feel free to comment! Tensorflow Tensorflow is more of an ecosystem, but you can import it into a python project and build some pretty amazing stuff relatively quickly. You usually see it imported as just tf: While it helps to have a basis in linear algebra and vector calculus that’s not entirely necessary. A tensor is an object that is similar to a vector but…

  • Programming,  Python

    Define docstrings in Python

    Bryson mentioned Docstrings in the latest episode of the MacAdmins Podcast. But how do you use them? Documentation strings (or docstrings for short) are an easy way to document Python objects (classes, functions, methods, and modules in-line with your code. Docstrings are created using three double-quotes in the first statement of the definition and are meant to describe in a human readable way what the object does. Let’s look at an example for hello_krypted: def hello_krypted(): """This simply echos Hello Krypted. But there's so much potential to do more! """ Docstrings can then be accessed using the __doc__ attribute on objects (e.g. via print): >>> print hello_krypted.__doc__ This simply echos…