<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Christian Lund</title>
    <description>The latest articles on DEV Community by Christian Lund (@lundchristian).</description>
    <link>https://dev.to/lundchristian</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1003008%2Fb586a387-ae10-45eb-ab2e-6769da336403.png</url>
      <title>DEV Community: Christian Lund</title>
      <link>https://dev.to/lundchristian</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lundchristian"/>
    <language>en</language>
    <item>
      <title>Python and ChatGPT</title>
      <dc:creator>Christian Lund</dc:creator>
      <pubDate>Mon, 09 Jan 2023 21:09:19 +0000</pubDate>
      <link>https://dev.to/lundchristian/python-and-chatgpt-363o</link>
      <guid>https://dev.to/lundchristian/python-and-chatgpt-363o</guid>
      <description>&lt;h2&gt;
  
  
  System used
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Windows 11&lt;/li&gt;
&lt;li&gt;Visual Studio Code&lt;/li&gt;
&lt;li&gt;Python&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Use ChatGPT in your Python script
&lt;/h2&gt;

&lt;p&gt;ChatGPT and other AI models are likely to be the future of software development, and a string of other fields. This is not a bad thing, it is just a great tool for us developers. Most likely you have heard all about it, and perhaps you've used the GUI provided on the OpenAI website. It is also possible to integrate the model in your script! Let's do so 😄&lt;/p&gt;

&lt;p&gt;The hierarchy&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;root/
├── env
└── project/
    └── main.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First of all I want to create a folder, in which a create and activate a virtual environment, as well as a &lt;em&gt;project&lt;/em&gt; folder where the &lt;code&gt;.py&lt;/code&gt; files go.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Note: The &lt;code&gt;#&lt;/code&gt; initiates a comment, so that bash command is explained&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PS C:\root\py_scripts&amp;gt; mkdir pyterminal #create application folder
PS C:\root\py_scripts&amp;gt; cd pyterminal #switch to said folder
PS C:\root\py_scripts\pyterminal&amp;gt; py -m venv env #create virtual environment
PS C:\root\py_scripts\pyterminal&amp;gt; env\Scripts\Activate.ps1 #activate said environment
PS C:\root\py_scripts\pyterminal&amp;gt; mkdir project #create folder for .py files
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the virtual environment is up and running, I install the &lt;code&gt;openai&lt;/code&gt; module, create a &lt;code&gt;main.py&lt;/code&gt; file and pop open visual studio code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(env) PS C:\root\py_scripts\pyterminal&amp;gt; pip install openai #install openai module
(env) PS C:\root\py_scripts\pyterminal&amp;gt; cd project #switch to project folder
(env) PS C:\root\py_scripts\pyterminal\project&amp;gt; new-item main.py #create .py file
(env) PS C:\root\py_scripts\pyterminal\project&amp;gt; cd.. #switch back
(env) PS C:\root\py_scripts\pyterminal&amp;gt; code . #open visual studio code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;main.py&lt;/code&gt; is content is shown down below. Four modules are imported:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;os&lt;/code&gt;, used to clear the terminal screen&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;json&lt;/code&gt;, used to parse json data&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;openai&lt;/code&gt;, well... that's why were here!&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;time/sleep&lt;/code&gt;, used to delay so that the ChatGPT response doesn't disappear immediately&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The function &lt;code&gt;configOpenAI()&lt;/code&gt; sets the API key. Get &lt;a href="https://beta.openai.com/account/api-keys" rel="noopener noreferrer"&gt;YOUR_API_KEY&lt;/a&gt;, to do so you will need to &lt;a href="https://openai.com/blog/chatgpt/" rel="noopener noreferrer"&gt;create an account&lt;/a&gt;. Do not share this key as it is personal information.&lt;/p&gt;

&lt;p&gt;The function &lt;code&gt;promptChatGPT(question)&lt;/code&gt; relies primarily on the &lt;code&gt;openai.Completion.create()&lt;/code&gt; function. The functions arguments are of importance:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Identifier of the model you want to use, in this case it is GPT-3. All models are found &lt;a href="https://beta.openai.com/docs/models/overview" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The prompt itself, a string or &lt;a href="https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them" rel="noopener noreferrer"&gt;tokens&lt;/a&gt; as OpenAI puts it.&lt;/li&gt;
&lt;li&gt;Maximal amount of tokens in return from the AI model, limit of 2048-4096.&lt;/li&gt;
&lt;li&gt;Temperature decides the &lt;em&gt;creativity&lt;/em&gt; of the response, ranged 0 to 1 where 1 is maximum.&lt;/li&gt;
&lt;li&gt;There are many arguments left unsaid, read the &lt;a href="https://beta.openai.com/docs/api-reference/completions/create" rel="noopener noreferrer"&gt;docs&lt;/a&gt; and let a magical future unfold.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The return value of said function is casted to a string, and subsequently the JSON is parsed as we only want the text response in return from ChatGPT.&lt;/p&gt;

&lt;p&gt;Lastly, I started out with &lt;em&gt;C/C++&lt;/em&gt;, therefore we have a 'main()' function. It is a simple continuous &lt;code&gt;while&lt;/code&gt; loop, where a menu is presented.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
import json
import openai
from time import sleep

def configOpenAI():
    openai.api_key = "YOUR_API_KEY"

def promptChatGPT(question="Test"):
    configOpenAI()
    chatGPT = "text-davinci-003"
    jsonData = openai.Completion.create(model=chatGPT, prompt=question, temperature=0, max_tokens=100)
    data = json.loads(str(jsonData))
    text = data['choices'][0]['text']
    print(text)

def main():
    choice = ""
    while True:
        os.system('cls')
        print("\nMenu:")
        print("\n[1] Prompt ChatGPT")
        print("[2] Quit")
        choice = input("Input: ")
        if choice == '1': promptChatGPT(input("Enter a prompt: "))
        elif choice == 'q': break
        sleep(5)

if __name__ == '__main__':
    main()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running script as, the program runs!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(env) PS C:\root\py_scripts\pyterminal\project&amp;gt; py main.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzjul3qsu8hey1lmi50zj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzjul3qsu8hey1lmi50zj.png" alt="Menu in terminal" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftqan84ea3rpkxwsum0q4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftqan84ea3rpkxwsum0q4.png" alt="Prompt in terminal" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The AI is the limit, happy coding!&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://beta.openai.com/docs/api-reference" rel="noopener noreferrer"&gt;OpenAI API Documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://openai.com/blog/chatgpt/" rel="noopener noreferrer"&gt;Header Image&lt;/a&gt;&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>Data types</title>
      <dc:creator>Christian Lund</dc:creator>
      <pubDate>Sun, 08 Jan 2023 12:42:25 +0000</pubDate>
      <link>https://dev.to/lundchristian/data-types-53b2</link>
      <guid>https://dev.to/lundchristian/data-types-53b2</guid>
      <description>&lt;h2&gt;
  
  
  &lt;em&gt;NOTE&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Specific data type implementation depends on the system, it's configuration, the programming language and probably a lot more. This post targets the data type in general, not the specific platform implementation. If you find it useful, or if you disagree, feel free to leave a comment&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;I’m new to software development, but I started out in a low-level world, with the traditional language C. As a beginner learning C, data types are important because all variables must be assigned accordingly, and even more so when dabbling in the world of embedded software. That’s why I felt quite frustrated when moving on to higher level languages like Python and JavaScript, where it is sufficient just writing &lt;code&gt;var&lt;/code&gt; or nothing at all. The interpreter infers the datatype, that does sound a bit sketchy to me.&lt;/p&gt;

&lt;p&gt;Reading this &lt;a href="https://dev.to/nasirovelchin/every-software-developer-should-write-a-blog-4622?fbclid=IwAR1GmL91eF4SRMpJK1NGqXQQjGzf9eiNmM4j3hdK-QEDDm3P99xUknCJqf0"&gt;blog post&lt;/a&gt;, I felt inspired to share some of my knowledge. The goal of this blog post is to go on a little journey through the basic data types, and find out how they differ, and why they are kind of all the same.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data
&lt;/h2&gt;

&lt;p&gt;No matter what type it is, the underlying format is always zero’s and one’s. This is crucial for my own understanding, modern computers process digital information as bits. Bits are the the components of binary numbers, and in more rigorous terms it is a base two system.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Base two: [0-1] in each position&lt;/li&gt;
&lt;li&gt;Base ten: [0-9] in each position&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For some reason we are familiar with base ten (perhaps our ten fingers), but for a computer base two is practical, as the voltage only have to be read as HIGH or LOW.&lt;/p&gt;

&lt;p&gt;The amount of bits determines the amount of different combinations the number can represent:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1bit: 2 different options&lt;/li&gt;
&lt;li&gt;2bit: 4 different options&lt;/li&gt;
&lt;li&gt;3bit: 8 different options&lt;/li&gt;
&lt;li&gt;4bit: 16 different options&lt;/li&gt;
&lt;li&gt;5bit: 32 different options&lt;/li&gt;
&lt;li&gt;6bit: 64 different options&lt;/li&gt;
&lt;li&gt;7bit: 128 different options&lt;/li&gt;
&lt;li&gt;8bit: 256 different options (aka. one byte)&lt;/li&gt;
&lt;li&gt;16bit: 2^16 different options (aka. &lt;code&gt;short&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;32bit: 2^32 different options (aka. &lt;code&gt;long&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;64bit: 2^64 different options (aka. &lt;code&gt;long long&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice the pattern, that often comes in handy when determining what data type to choose.&lt;/p&gt;

&lt;h2&gt;
  
  
  Type
&lt;/h2&gt;

&lt;p&gt;The type basically comes down to two terms: &lt;code&gt;signed&lt;/code&gt; and &lt;code&gt;unsigned&lt;/code&gt;. When unsigned, only zero and positive numbers can be represented, for 8 bits of data this means 0 to 255. The same 8 bits of data can be unsigned, in that case negative numbers are also represented, -127 to 128.&lt;/p&gt;

&lt;p&gt;There are different ways of doing this, but a common way is by two’s complement. The first bit is the sign, one is -1 and zero is +1. Say we take 65 and want its negative counterpart:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;0b 0100 0001&lt;/code&gt; = 65&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0b 0100 0001&lt;/code&gt; =(Flip all the bits)=&amp;gt; &lt;code&gt;0b 1011 1110&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0b 1011 1110&lt;/code&gt; =(Add one)=&amp;gt; &lt;code&gt;0b 1011 1111&lt;/code&gt; = -65&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Lastly, two more types must be identified, &lt;code&gt;integer&lt;/code&gt; and &lt;code&gt;decimal&lt;/code&gt;. Integers are pretty straightforward, only "whole" numbers can be represented, no fractions.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;1 OR 2 apples, NOT 1.5 apples&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The decimal fraction is represented in the &lt;code&gt;decimal&lt;/code&gt; group, often referred to as &lt;code&gt;float&lt;/code&gt; or &lt;code&gt;double&lt;/code&gt; depending on size. It is a bit more complex (great resource =&amp;gt; &lt;a href="https://en.wikipedia.org/wiki/Floating-point_arithmetic" rel="noopener noreferrer"&gt;Wikipage&lt;/a&gt;), but it still just a signed binary number, the difference is in the interpretation. A &lt;code&gt;decimal&lt;/code&gt; is split into two bit lengths; the &lt;em&gt;significand&lt;/em&gt; part represents the value, and the &lt;em&gt;exponent&lt;/em&gt; part represents where the decimal point should be placed. Everything else works the same way.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;1 OR 2 apples, OR 1.5 apples&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Data and type
&lt;/h2&gt;

&lt;p&gt;All data types that I'm aware of are derived from its bit size, sign convention and the interpretation of its value. But what about &lt;code&gt;string&lt;/code&gt;? That is just another way of interpreting a binary value, 65 for an example is the upper case letter A. Check out the &lt;a href="https://www.asciitable.com/" rel="noopener noreferrer"&gt;ASCII&lt;/a&gt; standard to see all commonly used characters. These characters are just &lt;code&gt;signed integer 8bits&lt;/code&gt;, often called &lt;code&gt;char&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;string&lt;/code&gt; is nothing more than a container that contains a given amount of &lt;code&gt;char&lt;/code&gt;, the same way an array can contain a given amount of any data type. Another case is the &lt;code&gt;bool&lt;/code&gt;, that is &lt;code&gt;false&lt;/code&gt; when zero, else it is &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Analyze a data type like this&lt;br&gt;
"sign convention" "number of bits" "interpretation of bits"&lt;br&gt;
Example =&amp;gt; &lt;code&gt;unsigned long int&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why data types matter
&lt;/h2&gt;

&lt;p&gt;When building software I consider size of importance regarding memory usage, and subsequently performance. Of course the machines of our modern world are incredibly powerful, and the way the hardware and operating system are configured matters a lot regarding bit size performance. But I don't see the point of using data types that are larger than what I need, there is no reason to waste potential clock cycles storing the value of one, using 64 bits.&lt;/p&gt;

&lt;p&gt;Control is probably even more important. Knowing what data types that are going in and out of functions, and assigning them accordingly massively improves readability, understanding and debugging. The last one because a lot of bugs won't even appear. The two first ones because you will get to see what data you're working with, and how you can manipulate it to your advantage.&lt;/p&gt;

&lt;p&gt;Even though data types essentially are the same, just a special configuration of eight or more bits. They all have strengths and weaknesses, and quite different use cases. Knowing data types, means knowing what tool to use for the job, and that is actually very useful. And personally I find liberating knowing that the system isn't actually that complex, just a series HIGH's and LOW's.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
