<?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: Timothy Huang</title>
    <description>The latest articles on DEV Community by Timothy Huang (@timhuang).</description>
    <link>https://dev.to/timhuang</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%2F406565%2Fa22c7b12-89e1-4f9d-b8d1-9e145e57f9ed.png</url>
      <title>DEV Community: Timothy Huang</title>
      <link>https://dev.to/timhuang</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/timhuang"/>
    <language>en</language>
    <item>
      <title>How to run Cisco Foundation-Sec-8B on Colab for FREE</title>
      <dc:creator>Timothy Huang</dc:creator>
      <pubDate>Fri, 23 Jan 2026 15:38:16 +0000</pubDate>
      <link>https://dev.to/timhuang/how-to-run-cisco-foundation-sec-8b-on-colab-for-free-43m5</link>
      <guid>https://dev.to/timhuang/how-to-run-cisco-foundation-sec-8b-on-colab-for-free-43m5</guid>
      <description>&lt;p&gt;Cisco Foundation-Sec-8B is an open source LLM model as an expert for security domain. Since the training data is including threat intelligence reports, vulnerability databases, incident response documentation, and security standards. Please find detail from the official huggingface website:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://huggingface.co/fdtn-ai/Foundation-Sec-8B" rel="noopener noreferrer"&gt;https://huggingface.co/fdtn-ai/Foundation-Sec-8B&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can easily setup an environment on Google Colab to run the example code on hugginface.&lt;/p&gt;

&lt;p&gt;First, go to colab and create a new file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://colab.research.google.com/#create=true" rel="noopener noreferrer"&gt;https://colab.research.google.com/#create=true&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the first code cell, please input the codes to setup an environment for the example code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;!pip install torch
!pip install git+https://github.com/huggingface/transformers
!pip install git+https://github.com/huggingface/accelerate
!pip install huggingface_hub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Click the left RUN button of the code cell, then you will see as below:&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%2Fm3mw4zirw37z41exq8o0.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%2Fm3mw4zirw37z41exq8o0.png" alt=" " width="800" height="471"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the setup finished, your environment is ready. Now we can put the example code to run. Please add an code cell and input the codes (from example part 1):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Import the required libraries
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

# Load the model and tokenizer
tokenizer = AutoTokenizer.from_pretrained("fdtn-ai/Foundation-Sec-8B")
model = AutoModelForCausalLM.from_pretrained("fdtn-ai/Foundation-Sec-8B")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Click the left RUN button of the 2nd code cell, then you will see as below: (the download process of LLM model 16GB may need more than 5 minutes)&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%2Feqpi8mvtm2xgb44fyhg7.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%2Feqpi8mvtm2xgb44fyhg7.png" alt=" " width="800" height="476"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, we can put another code cell and input the code (from example part 2):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Example: Matching CWE to CVE IDs
prompt="""CVE-2021-44228 is a remote code execution flaw in Apache Log4j2 via unsafe JNDI lookups (“Log4Shell”). The CWE is CWE-502.

CVE-2017-0144 is a remote code execution vulnerability in Microsoft’s SMBv1 server (“EternalBlue”) due to a buffer overflow. The CWE is CWE-119.

CVE-2014-0160 is an information-disclosure bug in OpenSSL’s heartbeat extension (“Heartbleed”) causing out-of-bounds reads. The CWE is CWE-125.

CVE-2017-5638 is a remote code execution issue in Apache Struts 2’s Jakarta Multipart parser stemming from improper input validation of the Content-Type header. The CWE is CWE-20.

CVE-2019-0708 is a remote code execution vulnerability in Microsoft’s Remote Desktop Services (“BlueKeep”) triggered by a use-after-free. The CWE is CWE-416.

CVE-2015-10011 is a vulnerability about OpenDNS OpenResolve improper log output neutralization. The CWE is"""

# Tokenize the input
inputs = tokenizer(prompt, return_tensors="pt")

# Generate the response
outputs = model.generate(
    inputs["input_ids"],
    max_new_tokens=3,
    do_sample=True,
    temperature=0.1,
    top_p=0.9,
)

# Decode and print the response
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
response = response.replace(prompt, "").strip()
print(response)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Click the left RUN button of the 3rd code cell, then you will see as below: (Since this is the inference process, so we will wait for some time, in my case is 7 minutes), and the result is the same as example  code expectation "CWE-117"&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%2F5yp3hyo1zq9pfuqd15pa.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%2F5yp3hyo1zq9pfuqd15pa.png" alt=" " width="800" height="601"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to use a GPU to speed up the inference process, you can upgrade Colab and connect to a GPU-enabled machine (which requires payment), which will significantly reduce inference time.&lt;/p&gt;

&lt;p&gt;Reference and thanks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/nodeshiftcloud/how-to-install-foundation-sec-8b-by-cisco-the-ultimate-cybersecurity-ai-model-2j9i"&gt;https://dev.to/nodeshiftcloud/how-to-install-foundation-sec-8b-by-cisco-the-ultimate-cybersecurity-ai-model-2j9i&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blogs.cisco.com/security/foundation-sec-cisco-foundation-ai-first-open-source-security-model" rel="noopener noreferrer"&gt;https://blogs.cisco.com/security/foundation-sec-cisco-foundation-ai-first-open-source-security-model&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;I've tried to use Colab with L4 GPU, the inference process need only 1 second to print out the result as below:&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%2F0anu0g5c1bdogpzk25yn.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%2F0anu0g5c1bdogpzk25yn.png" alt=" " width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(T4 is free, but vRAM is not enough to load the model, the minimum GPU on Colab is L4 to load this model.)&lt;/p&gt;

</description>
      <category>ai</category>
      <category>security</category>
      <category>llm</category>
    </item>
    <item>
      <title>An easy way to keep iframe with 16:9 aspect ratio without outer div (Pure CSS)</title>
      <dc:creator>Timothy Huang</dc:creator>
      <pubDate>Wed, 22 Jun 2022 03:31:40 +0000</pubDate>
      <link>https://dev.to/timhuang/an-easy-way-to-keep-iframe-with-169-aspect-ratio-without-another-div-pure-css-2h38</link>
      <guid>https://dev.to/timhuang/an-easy-way-to-keep-iframe-with-169-aspect-ratio-without-another-div-pure-css-2h38</guid>
      <description>&lt;p&gt;Youtube video provides a embedded code to let us embed iframe in our web page. However, we need the youtube video on the page to keep aspect ratio 16:9.&lt;/p&gt;

&lt;p&gt;There is an easy way to do this, a pure css code aspect-ratio property as below code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;iframe {
  width: 100%;
  height: auto;
  aspect-ratio: 16 / 9;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We keep the width 100% and height auto, and also keep the aspect ratio with 16:9. &lt;/p&gt;

&lt;p&gt;There is an example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codepen.io/timhuang/pen/BaYdMmN"&gt;https://codepen.io/timhuang/pen/BaYdMmN&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And also check the aspect-ratio property browser compatibility: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://caniuse.com/mdn-css_properties_aspect-ratio"&gt;https://caniuse.com/mdn-css_properties_aspect-ratio&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Most browsers support this property, but some old browsers don't support, we can use old way with an outer div, also check this post:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/jh3y/css-aspect-ratio-2k6o"&gt;https://dev.to/jh3y/css-aspect-ratio-2k6o&lt;/a&gt; (Thanks Tompkins &lt;a href="https://dev.to/jh3y"&gt;https://dev.to/jh3y&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Happy coding!!&lt;/p&gt;

</description>
      <category>css</category>
      <category>html</category>
    </item>
    <item>
      <title>How to run a flask project on Repl.it? (with a brower preview)</title>
      <dc:creator>Timothy Huang</dc:creator>
      <pubDate>Fri, 15 Oct 2021 06:13:39 +0000</pubDate>
      <link>https://dev.to/timhuang/how-to-run-a-flask-project-on-replit-with-a-brower-preview-28m1</link>
      <guid>https://dev.to/timhuang/how-to-run-a-flask-project-on-replit-with-a-brower-preview-28m1</guid>
      <description>&lt;p&gt;Repl.it (&lt;a href="https://repl.it/" rel="noopener noreferrer"&gt;https://repl.it/&lt;/a&gt;) is a very useful development runtime environment. We can easily run nodejs, php, html, python, ..etc. on repl.it.&lt;/p&gt;

&lt;p&gt;But how to run a flask project on repl.it? We can refer to quick start on flask document: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://flask.palletsprojects.com/en/2.0.x/quickstart/" rel="noopener noreferrer"&gt;https://flask.palletsprojects.com/en/2.0.x/quickstart/&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "&amp;lt;p&amp;gt;Hello, World!&amp;lt;/p&amp;gt;"


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;But when we push "Run" button on the header area on repl.it, the console start to build and run and exit with nothing, sure, we didn't use flask command to start the program.&lt;/p&gt;

&lt;p&gt;Switch to Shell, run:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

$ export FLASK_APP=main
$ flask run 
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We can find that it starts to listen port 5000, but we can't read the result content.&lt;/p&gt;

&lt;p&gt;So, how to read the content? Just put the line on the bottom of these code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

app.run(host='0.0.0.0')


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then push the "Run" button, the web server listen on 0.0.0.0:5000 and start a new window that the browser preview content comes out. It's really convenient to develop python flask project on repl.it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F1maojqb0t9z9jnco232n.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F1maojqb0t9z9jnco232n.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The demo project is here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://replit.com/@timhuangt/demoflask" rel="noopener noreferrer"&gt;https://replit.com/@timhuangt/demoflask&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's play with flask on Repl.it.&lt;/p&gt;

</description>
      <category>python</category>
      <category>flask</category>
      <category>webserver</category>
    </item>
    <item>
      <title>A css-selector that a parent with child</title>
      <dc:creator>Timothy Huang</dc:creator>
      <pubDate>Wed, 16 Sep 2020 06:07:47 +0000</pubDate>
      <link>https://dev.to/timhuang/css-select-a-parent-with-condition-452m</link>
      <guid>https://dev.to/timhuang/css-select-a-parent-with-condition-452m</guid>
      <description>&lt;p&gt;Sometimes we want to select elements with known child, we can use :has() selector, but no browser support now. (just check can i use :has() - &lt;a href="https://caniuse.com/?search=%3Ahas()"&gt;https://caniuse.com/?search=%3Ahas()&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;However, we can use jQuery to select these elements by :has() like $("div:has(span.red)").&lt;/p&gt;

&lt;p&gt;There is an example to show how it works:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codepen.io/timhuang/pen/jOqpxbY"&gt;https://codepen.io/timhuang/pen/jOqpxbY&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$("div:has(span.red)").append("&amp;lt;p&amp;gt;this paragraph was added by jquery&amp;lt;/p&amp;gt;"); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;jQuery is support the :has() pseudo-class just a draft on W3C: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://drafts.csswg.org/selectors-4/#relational"&gt;https://drafts.csswg.org/selectors-4/#relational&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the draft turning into specification we can use :has(), maybe css 4.&lt;/p&gt;

</description>
      <category>css</category>
      <category>html</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Check if file modified in n minutes</title>
      <dc:creator>Timothy Huang</dc:creator>
      <pubDate>Mon, 07 Sep 2020 04:22:31 +0000</pubDate>
      <link>https://dev.to/timhuang/check-if-file-modified-in-n-minutes-eh</link>
      <guid>https://dev.to/timhuang/check-if-file-modified-in-n-minutes-eh</guid>
      <description>&lt;p&gt;Sometime we want to check if file modified in n minutes and do something with each file, we can easily use some commands.&lt;/p&gt;

&lt;p&gt;Use test to check if condition, find with -mmin to check if older than n minutes, and for-do-done loop to enumerate each file in *.txt&lt;/p&gt;

&lt;p&gt;Ok, let's put all together,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# check if file modified in 40 mins
for f in *.txt
do
  if ! test `find $f -mmin +40`
  then
    echo $f
  fi
done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;for f in *.txt is to enumerate each file with txt extension file, and if ! is for check the false condition with find $f -mmin +40, i.e. $f not older than 40 minutes, also means $f is as new as modified in 40 minutes.&lt;/p&gt;

&lt;p&gt;There is a example test code to check:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://repl.it/@timhuangt/checkIfFileModified"&gt;https://repl.it/@timhuangt/checkIfFileModified&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just create 3 files (with touch command) that modified in now, 30 minutes ago, 60 minutes ago, and with the modified date-time filename with each file. After the for-do-done loop, we can echo the file(s) name modified in 40 minutes.&lt;/p&gt;

&lt;p&gt;Reference:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/552724/how-do-i-check-in-bash-whether-a-file-was-created-more-than-x-time-ago"&gt;https://stackoverflow.com/questions/552724/how-do-i-check-in-bash-whether-a-file-was-created-more-than-x-time-ago&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://unix.stackexchange.com/questions/118577/changing-a-files-date-created-and-last-modified-attributes-to-another-file/250407"&gt;https://unix.stackexchange.com/questions/118577/changing-a-files-date-created-and-last-modified-attributes-to-another-file/250407&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/16142973/add-some-specific-time-while-using-the-linux-command-date"&gt;https://stackoverflow.com/questions/16142973/add-some-specific-time-while-using-the-linux-command-date&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>bash</category>
      <category>file</category>
    </item>
    <item>
      <title>Sum of nature numbers with JavaScript (3 ways)</title>
      <dc:creator>Timothy Huang</dc:creator>
      <pubDate>Tue, 14 Jul 2020 16:15:31 +0000</pubDate>
      <link>https://dev.to/timhuang/sum-of-nature-numbers-with-javascript-1mg1</link>
      <guid>https://dev.to/timhuang/sum-of-nature-numbers-with-javascript-1mg1</guid>
      <description>&lt;p&gt;There are many ways to calculate sum of nature numbers. The basic method is for-loop (a simple way):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sum1(n){
  var sum = 0;
  for(var i=1;i&amp;lt;=n;i++){
    sum += i;
  }
  return sum;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another method is recursion. Since the sum is 1 + 2 + .. + n, we can easily get &lt;/p&gt;

&lt;p&gt;sum(n) = n + sum(n-1) &lt;br&gt;
and sum(n-1) = n-1 + sum(n-2) ... &lt;br&gt;
until sum(1) = 1. &lt;/p&gt;

&lt;p&gt;This also is a divide-and-conquer method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sum2(n){
  if(n==1){
    return 1;
  }else{
    return n + sum2(n-1);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The third way is more complex, using map and reduce. With JavaScript, the map function and reduce function are Array methods. &lt;/p&gt;

&lt;p&gt;First, we can use map function to create an array as [1, 2, ... n] :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[...Array(n)]
    .map(function(_, index){ 
      return index + 1; 
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Second, we can use reduce function to calculate the sum of these numbers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[...Array(n)]
    .map(function(_, index){ 
      return index + 1; 
    })
    .reduce(function(acc, cur){
      return acc + cur;
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's play with repl.it: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://repl.it/@timhuangt/sumOfNatureNumbers"&gt;https://repl.it/@timhuangt/sumOfNatureNumbers&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>math</category>
    </item>
    <item>
      <title>Get top 50 web traffic sites with Python</title>
      <dc:creator>Timothy Huang</dc:creator>
      <pubDate>Tue, 07 Jul 2020 07:46:48 +0000</pubDate>
      <link>https://dev.to/timhuang/get-top-50-web-traffic-sites-with-python-512l</link>
      <guid>https://dev.to/timhuang/get-top-50-web-traffic-sites-with-python-512l</guid>
      <description>&lt;p&gt;We can get the top 50 web traffic sties with these two site traffic monitor services:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Alexa: &lt;a href="https://www.alexa.com/"&gt;https://www.alexa.com/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;SimilarWeb: &lt;a href="https://www.similarweb.com/"&gt;https://www.similarweb.com/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and the top 50 web traffic sites are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Alexa: &lt;a href="https://www.alexa.com/topsites"&gt;https://www.alexa.com/topsites&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;SimilarWeb: &lt;a href="https://www.similarweb.com/top-websites/"&gt;https://www.similarweb.com/top-websites/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With python requests and BeautifulSoup modules, we can automate list the top 50 web sites from these two monitor services.&lt;/p&gt;

&lt;p&gt;First we create a dict that store these two monitor service urls and selectors (for BeautifulSoup select):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;webRankSites&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="s"&gt;"Alexa"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"https://www.alexa.com/topsites/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"selector"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"div.DescriptionCell"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="s"&gt;"SimilarWeb"&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;
    &lt;span class="s"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"https://www.similarweb.com/top-websites/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"selector"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"td.topRankingGrid-cell.topWebsitesGrid-cellWebsite.showInMobile"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How to define the selector? We need to check these two services url content with the site list:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Alexa:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LF--zwl4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lq7s00g0hv8jqo18djy2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LF--zwl4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lq7s00g0hv8jqo18djy2.png" alt="Alexa selector" width="880" height="509"&gt;&lt;/a&gt;&lt;br&gt;
As the developer tools show, the web site is in the element div with class DescriptionCell, the selector is "div.DescriptionCell".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;SimilarWeb:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1oz8amtd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bnb6ssktxfgr31pjzjrb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1oz8amtd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bnb6ssktxfgr31pjzjrb.png" alt="SimilarWeb selector" width="880" height="662"&gt;&lt;/a&gt;&lt;br&gt;
The web site is in the element td with 3 classes topRankingGrid-cell, topWebsitesGrid-cellWebsite, showInMobile. The selector is "td.topRankingGrid-cell.topWebsitesGrid-cellWebsite.showInMobile".&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Second we start to get the url content with requests.get and with BeautifulSoup selector patterns to get the web site list (myheaders is used for similarWeb service, since no user-agent will result response status code 403):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="n"&gt;myheaders&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"user-agent"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0(HTTP_USER_AGENT)"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;site&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;webRankSites&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"site: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;resp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;webRankSites&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myheaders&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;soup&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;BeautifulSoup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'html.parser'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;soup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;webRankSites&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s"&gt;"selector"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;". "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we can get the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;site: Alexa
1. Google.com
2. Youtube.com
3. Tmall.com
...
site: SimilarWeb
1. google.com
2. youtube.com
3. facebook.com
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wow, the result can be automate to get and it looks great. Wanna try? Check this demo: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://repl.it/@timhuangt/GlobalTopSite"&gt;https://repl.it/@timhuangt/GlobalTopSite&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And enjoy it! Happy coding!!&lt;/p&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>List comprehension in Python</title>
      <dc:creator>Timothy Huang</dc:creator>
      <pubDate>Sat, 04 Jul 2020 08:30:26 +0000</pubDate>
      <link>https://dev.to/timhuang/list-comprehension-in-python-5595</link>
      <guid>https://dev.to/timhuang/list-comprehension-in-python-5595</guid>
      <description>&lt;p&gt;Sometimes we need a list with a serial integers, we can use the list comprehension syntax to generate a list with a serial integers like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = [ x for x in range(0,100) ]
# a = [0, 1, 2, ... 99]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we need a list with some condition, like even numbers. We can also use the list comprehension syntax to generate a list with even numbers, like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = [ x for x in range(0, 100) if x % 2 == 0]
# a = [0, 2, 4, ... 98]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another case we need a list with tuple like 2-dimension coordinate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = [ (x, y) for x in range(1, 6) for y in range(3, 6) ]
# a = [ (1, 3), (1, 4), (1, 5), (2, 3), (2, 4) ... ] 
## total 5 * 3 items 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if we need the value pairs with both index with x, y move simultaneously, we can use zip:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = [ x for x in zip(range(1, 6), range(3, 6)) ]
# a = [(1, 3), (2, 4), (3, 5)]
## total 3 items, because the range(3, 6)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same usage with list comprehension, set and dictionary also has their comprehension.&lt;/p&gt;

&lt;h1&gt;
  
  
  set comprehension:
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;s = { v for v in 'ABCDE' }
# s = {'D', 'C', 'A', 'B', 'E'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  with some condition:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;s = { v for v in 'ABCDE' if v not in 'BC' }
# {'D', 'E', 'A'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  dictionary comprehension:
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;s = {key: val for key, val in enumerate('ABCDEF')}
# s = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  with some condition:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;s = {key: val for key, val in enumerate('ABCDEF') if val not in 'CD'}
# s = {0: 'A', 1: 'B', 4: 'E', 5: 'F'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  with some condition (more complex):
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;s = {key: val for key, val in enumerate('ABCDEF') if (val not in 'CD') &amp;amp; (key != 0)}
# s = {1: 'B', 4: 'E', 5: 'F'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After these example, we can understand the list comprehension to generate list and other types of data.&lt;/p&gt;

&lt;p&gt;It's simple but important!&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

&lt;p&gt;Reference: &lt;a href="https://en.wikipedia.org/wiki/List_comprehension"&gt;https://en.wikipedia.org/wiki/List_comprehension&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>A simple way to detect if browser is on a mobile device with Javascript</title>
      <dc:creator>Timothy Huang</dc:creator>
      <pubDate>Tue, 30 Jun 2020 03:45:10 +0000</pubDate>
      <link>https://dev.to/timhuang/a-simple-way-to-detect-if-browser-is-on-a-mobile-device-with-javascript-44j3</link>
      <guid>https://dev.to/timhuang/a-simple-way-to-detect-if-browser-is-on-a-mobile-device-with-javascript-44j3</guid>
      <description>&lt;p&gt;Sometimes we need a little Javascript code snippet to detect if user use mobile device, the simplest way is detecting its browser user agent.&lt;/p&gt;

&lt;p&gt;We use the regular expression test to detect if browser is a mobile device like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userAgent&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
  &lt;span class="c1"&gt;// true for mobile device&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mobile device&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// false for not mobile device&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;not mobile device&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;demo is on codepen:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codepen.io/timhuang/pen/MWKEZMJ"&gt;https://codepen.io/timhuang/pen/MWKEZMJ&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Reference:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/3514784/what-is-the-best-way-to-detect-a-mobile-device"&gt;https://stackoverflow.com/questions/3514784/what-is-the-best-way-to-detect-a-mobile-device&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>mobile</category>
      <category>browser</category>
    </item>
    <item>
      <title>dev is really a good place for share dev knowledge</title>
      <dc:creator>Timothy Huang</dc:creator>
      <pubDate>Thu, 11 Jun 2020 06:02:47 +0000</pubDate>
      <link>https://dev.to/timhuang/dev-is-really-a-good-place-for-share-dev-knowledge-1emp</link>
      <guid>https://dev.to/timhuang/dev-is-really-a-good-place-for-share-dev-knowledge-1emp</guid>
      <description>&lt;p&gt;hello there,&lt;/p&gt;

&lt;p&gt;this is the very first post on my dev.to/timhuang place. thanks you for providing such a good place to share dev knowledge.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
