kandi background
Explore Kits

zx | A tool for writing better scripts | Runtime Evironment library

 by   google JavaScript Version: 6.1.0 License: Apache-2.0

 by   google JavaScript Version: 6.1.0 License: Apache-2.0

Download this library from

kandi X-RAY | zx Summary

zx is a JavaScript library typically used in Server, Runtime Evironment, Nodejs applications. zx has no bugs, it has no vulnerabilities, it has a Permissive License and it has medium support. You can install using 'npm i zx' or download it from GitHub, npm.
Bash is great, but when it comes to writing scripts, people usually choose a more convenient programming language. JavaScript is a perfect choice, but standard Node.js library requires additional hassle before using. The zx package provides useful wrappers around child_process, escapes arguments and gives sensible defaults.
Support
Support
Quality
Quality
Security
Security
License
License
Reuse
Reuse

kandi-support Support

  • zx has a medium active ecosystem.
  • It has 30481 star(s) with 682 fork(s). There are 131 watchers for this library.
  • There were 1 major release(s) in the last 6 months.
  • There are 10 open issues and 205 have been closed. On average issues are closed in 6 days. There are 1 open pull requests and 0 closed requests.
  • It has a neutral sentiment in the developer community.
  • The latest version of zx is 6.1.0
zx Support
Best in #Runtime Evironment
Average in #Runtime Evironment
zx Support
Best in #Runtime Evironment
Average in #Runtime Evironment

quality kandi Quality

  • zx has 0 bugs and 0 code smells.
zx Quality
Best in #Runtime Evironment
Average in #Runtime Evironment
zx Quality
Best in #Runtime Evironment
Average in #Runtime Evironment

securitySecurity

  • zx has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
  • zx code analysis shows 0 unresolved vulnerabilities.
  • There are 0 security hotspots that need review.
zx Security
Best in #Runtime Evironment
Average in #Runtime Evironment
zx Security
Best in #Runtime Evironment
Average in #Runtime Evironment

license License

  • zx is licensed under the Apache-2.0 License. This license is Permissive.
  • Permissive licenses have the least restrictions, and you can use them in most projects.
zx License
Best in #Runtime Evironment
Average in #Runtime Evironment
zx License
Best in #Runtime Evironment
Average in #Runtime Evironment

buildReuse

  • zx releases are available to install and integrate.
  • Deployable package is available in npm.
  • Installation instructions, examples and code snippets are available.
zx Reuse
Best in #Runtime Evironment
Average in #Runtime Evironment
zx Reuse
Best in #Runtime Evironment
Average in #Runtime Evironment
Top functions reviewed by kandi - BETA

Coming Soon for all Libraries!

Currently covering the most popular Java, JavaScript and Python libraries. See a SAMPLE HERE.
kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.

zx Key Features

A tool for writing better scripts

🐚 zx

copy iconCopydownload iconDownload
#!/usr/bin/env zx

await $`cat package.json | grep name`

let branch = await $`git branch --show-current`
await $`dep deploy --branch=${branch}`

await Promise.all([
  $`sleep 1; echo 1`,
  $`sleep 2; echo 2`,
  $`sleep 3; echo 3`,
])

let name = 'foo bar'
await $`mkdir /tmp/${name}`

Install

copy iconCopydownload iconDownload
npm i -g zx

Documentation

copy iconCopydownload iconDownload
#!/usr/bin/env zx

Functions

copy iconCopydownload iconDownload
cd('/tmp')
await $`pwd` // outputs /tmp

Packages

copy iconCopydownload iconDownload
console.log(chalk.blue('Hello world!'))

Configuration

copy iconCopydownload iconDownload
$.shell = '/usr/bin/bash'

Polyfills

copy iconCopydownload iconDownload
let {version} = require('./package.json')

Experimental

copy iconCopydownload iconDownload
import {retry} from 'zx/experimental'

let {stdout} = await retry(5)`curl localhost`

FAQ

copy iconCopydownload iconDownload
process.env.FOO = 'bar'
await $`echo $FOO`

Slow code when fetching data from Coinmarketcap api

copy iconCopydownload iconDownload
import asyncio
import time

import requests
import httpx


def test_sync():
    for _ in range(50):
        requests.get("https://google.com")


async def test_async():
    async with httpx.AsyncClient() as client:
        tasks = [client.get("https://google.com") for _ in range(50)]
        await asyncio.gather(*tasks)


if __name__ == "__main__":
    start = time.process_time()
    test_sync()
    print(f"sync test took {time.process_time() - start} s")

    start = time.process_time()
    asyncio.run(test_async())
    print(f"async test took {time.process_time() - start} s")


# sync test took 23.0833 s
# async test took 0.2662 s

Error message caught by Axios is undefined in production, but works locally?

copy iconCopydownload iconDownload
server:
  error:
  include-message: always

Checking different elements of a cell in dataframe separated by comma using Pandas

copy iconCopydownload iconDownload
Accepted = set(df1.to_numpy().ravel())
Ignored = set(df2.to_numpy().ravel())
Both = set.union(Accepted,Ignored)

df['Accepted'] = df['Subset'].apply(lambda x: ','.join(Accepted & set(x.split(','))))
df['Ignored'] = df['Subset'].apply(lambda x:  ','.join(Ignored & set(x.split(','))))
df['Exception'] = df['Subset'].apply(lambda x:  ','.join(set(x.split(',')) - Both))

print (df)
    Name      Subset Accepted Ignored Exception
0  Apple   AC,AD,AGH   AC,AGH      AD          
1    Bat  ZX,ADS,AXC       ZX     ADS       AXC
2    Cat      HJ,AGH      AGH                HJ
-----------------------
accepted_list = [item for sublist in accepted_df.values.tolist() for item in sublist]
ignored_list = [item for sublist in ignored_df.values.tolist() for item in sublist]

input_df["Subset"] = input_df["Subset"].apply(lambda x: x.split(","))
input_df["Accepted"] = input_df["Subset"].apply(lambda x: [i for i in x if i in accepted_list])
input_df["Ignored"] = input_df["Subset"].apply(lambda x: [i for i in x if i in ignored_list])
input_df["Exception"] = input_df["Subset"].apply(lambda x: [i for i in x if i not in accepted_list + ignored_list])
-----------------------
cat = (pd.concat({'Accepted': accepted, 'Ignored': ignored})
       .stack()
       .reset_index()
       .set_index(0)['level_0'].to_dict()
    )
# {'AC': 'Accepted', 'ASD': 'Accepted', 'ZX': 'Accepted',
#  'AGH': 'Accepted', 'AD': 'Ignored', 'ADS': 'Ignored'}

(inp.assign(col=inp['Subset'].str.split(','))
    .explode('col')
    .assign(value=lambda d: d['col'].map(lambda x: cat.get(x, 'Exception')))
    .pivot_table(index=['Name', 'Subset'], columns='value', values='col',
                 aggfunc=','.join, fill_value='')
    .reset_index().rename_axis(None, axis=1)
)
    Name      Subset Accepted Exception Ignored
0  Apple   AC,AD,AGH   AC,AGH                AD
1    Bat  ZX,ADS,AXC       ZX       AXC     ADS
2    Cat      HJ,AGH      AGH        HJ        
-----------------------
cat = (pd.concat({'Accepted': accepted, 'Ignored': ignored})
       .stack()
       .reset_index()
       .set_index(0)['level_0'].to_dict()
    )
# {'AC': 'Accepted', 'ASD': 'Accepted', 'ZX': 'Accepted',
#  'AGH': 'Accepted', 'AD': 'Ignored', 'ADS': 'Ignored'}

(inp.assign(col=inp['Subset'].str.split(','))
    .explode('col')
    .assign(value=lambda d: d['col'].map(lambda x: cat.get(x, 'Exception')))
    .pivot_table(index=['Name', 'Subset'], columns='value', values='col',
                 aggfunc=','.join, fill_value='')
    .reset_index().rename_axis(None, axis=1)
)
    Name      Subset Accepted Exception Ignored
0  Apple   AC,AD,AGH   AC,AGH                AD
1    Bat  ZX,ADS,AXC       ZX       AXC     ADS
2    Cat      HJ,AGH      AGH        HJ        

Keep only characters in between two hyphens from a dataframe cell which has comma separated list

copy iconCopydownload iconDownload
In [2193]: df['Subset'] = df.Subset.str.split(',').explode().str.split('-').str[:2].apply(''.join).groupby(level=0).agg(','.join)

In [2194]: df
Out[2194]: 
    Name       Subset
0  Apple  AI,BI,XC,ZX
1    Bat     po,IJ,IA
-----------------------
df['Subset'] = (df['Subset'].str.extractall(r'-([^-]+)-')[0]
                .groupby(level=0).agg(','.join)
                )
    Name    Subset
0  Apple  AI,BI,XC
1    Bat  po,IJ,IA
df['Subset'] = (df['Subset'].str.extractall(r'[-,]+([^-,]+)-')[0]
                .groupby(level=0).agg(','.join))
    Name       Subset
0  Apple  AI,BI,XC,ZX
1    Bat     po,IJ,I
-----------------------
df['Subset'] = (df['Subset'].str.extractall(r'-([^-]+)-')[0]
                .groupby(level=0).agg(','.join)
                )
    Name    Subset
0  Apple  AI,BI,XC
1    Bat  po,IJ,IA
df['Subset'] = (df['Subset'].str.extractall(r'[-,]+([^-,]+)-')[0]
                .groupby(level=0).agg(','.join))
    Name       Subset
0  Apple  AI,BI,XC,ZX
1    Bat     po,IJ,I
-----------------------
df['Subset'] = (df['Subset'].str.extractall(r'-([^-]+)-')[0]
                .groupby(level=0).agg(','.join)
                )
    Name    Subset
0  Apple  AI,BI,XC
1    Bat  po,IJ,IA
df['Subset'] = (df['Subset'].str.extractall(r'[-,]+([^-,]+)-')[0]
                .groupby(level=0).agg(','.join))
    Name       Subset
0  Apple  AI,BI,XC,ZX
1    Bat     po,IJ,I
-----------------------
df['Subset'] = (df['Subset'].str.extractall(r'-([^-]+)-')[0]
                .groupby(level=0).agg(','.join)
                )
    Name    Subset
0  Apple  AI,BI,XC
1    Bat  po,IJ,IA
df['Subset'] = (df['Subset'].str.extractall(r'[-,]+([^-,]+)-')[0]
                .groupby(level=0).agg(','.join))
    Name       Subset
0  Apple  AI,BI,XC,ZX
1    Bat     po,IJ,I

RenderTexture transparency working in editor becomes solid color in build

copy iconCopydownload iconDownload
[numthreads(16,16,1)]
void SetTexture (uint3 id : SV_DispatchThreadID)
{
    Result[id.xy] = float4(0.0, 0.0, 0.0, 0.0);
}

microk8s coredns CrashLoopBackOff

copy iconCopydownload iconDownload
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
data:
  Corefile: |
    .:53 {
        errors
        health {
          lameduck 5s
        }
        ready
        log . {
          class error
        }
        kubernetes cluster.local in-addr.arpa ip6.arpa {
          pods insecure
          fallthrough in-addr.arpa ip6.arpa
        }
        prometheus :9153
        forward . 8.8.8.8 8.8.4.4
        reload
        loadbalance
    }
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"Corefile":".:53 {\n    errors\n    health {\n      lameduck 5s\n    }\n    ready\n    log . {\n      class error\n    }\n    kubernetes cluster.local in-addr.arpa ip6.arpa {\n      pods insecure\n      fallthrough in-addr.arpa ip6.arpa\n    }\n    prometheus :9153\n    forward . 8.8.8.8 8.8.4.4    **reload**\n    loadbalance\n}\n"},"kind":"ConfigMap","metadata":{"annotations":{},"creationTimestamp":"2022-01-25T09:35:32Z","labels":{"addonmanager.kubernetes.io/mode":"EnsureExists","k8s-app":"kube-dns"},"managedFields":[{"apiVersion":"v1","fieldsType":"FieldsV1","fieldsV1":{"f:data":{".":{},"f:Corefile":{}},"f:metadata":{"f:annotations":{".":{},"f:kubectl.kubernetes.io/last-applied-configuration":{}},"f:labels":{".":{},"f:addonmanager.kubernetes.io/mode":{},"f:k8s-app":{}}}},"manager":"kubectl-client-side-apply","operation":"Update","time":"2022-01-25T09:35:32Z"}],"name":"coredns","namespace":"kube-system","resourceVersion":"518","selfLink":"/api/v1/namespaces/kube-system/configmaps/coredns","uid":"8a73848a-4118-417b-80f5-2175bb64acc4"}}
  creationTimestamp: "2022-01-25T09:35:32Z"
  labels:
    addonmanager.kubernetes.io/mode: EnsureExists
    k8s-app: kube-dns
  name: coredns
  namespace: kube-system
  resourceVersion: "18764"
  selfLink: /api/v1/namespaces/kube-system/configmaps/coredns
  uid: 8a73848a-4118-417b-80f5-2175bb64acc4
~                                                                                                                                                            ~                                                                                                                                                            ~                                                                                                                                                            ~                                             
sudo microk8s.kubectl -n kube-system edit configmaps coredns -o yaml
sudo microk8s.kubectl -n kube-system rollout restart deploy
-----------------------
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
data:
  Corefile: |
    .:53 {
        errors
        health {
          lameduck 5s
        }
        ready
        log . {
          class error
        }
        kubernetes cluster.local in-addr.arpa ip6.arpa {
          pods insecure
          fallthrough in-addr.arpa ip6.arpa
        }
        prometheus :9153
        forward . 8.8.8.8 8.8.4.4
        reload
        loadbalance
    }
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"Corefile":".:53 {\n    errors\n    health {\n      lameduck 5s\n    }\n    ready\n    log . {\n      class error\n    }\n    kubernetes cluster.local in-addr.arpa ip6.arpa {\n      pods insecure\n      fallthrough in-addr.arpa ip6.arpa\n    }\n    prometheus :9153\n    forward . 8.8.8.8 8.8.4.4    **reload**\n    loadbalance\n}\n"},"kind":"ConfigMap","metadata":{"annotations":{},"creationTimestamp":"2022-01-25T09:35:32Z","labels":{"addonmanager.kubernetes.io/mode":"EnsureExists","k8s-app":"kube-dns"},"managedFields":[{"apiVersion":"v1","fieldsType":"FieldsV1","fieldsV1":{"f:data":{".":{},"f:Corefile":{}},"f:metadata":{"f:annotations":{".":{},"f:kubectl.kubernetes.io/last-applied-configuration":{}},"f:labels":{".":{},"f:addonmanager.kubernetes.io/mode":{},"f:k8s-app":{}}}},"manager":"kubectl-client-side-apply","operation":"Update","time":"2022-01-25T09:35:32Z"}],"name":"coredns","namespace":"kube-system","resourceVersion":"518","selfLink":"/api/v1/namespaces/kube-system/configmaps/coredns","uid":"8a73848a-4118-417b-80f5-2175bb64acc4"}}
  creationTimestamp: "2022-01-25T09:35:32Z"
  labels:
    addonmanager.kubernetes.io/mode: EnsureExists
    k8s-app: kube-dns
  name: coredns
  namespace: kube-system
  resourceVersion: "18764"
  selfLink: /api/v1/namespaces/kube-system/configmaps/coredns
  uid: 8a73848a-4118-417b-80f5-2175bb64acc4
~                                                                                                                                                            ~                                                                                                                                                            ~                                                                                                                                                            ~                                             
sudo microk8s.kubectl -n kube-system edit configmaps coredns -o yaml
sudo microk8s.kubectl -n kube-system rollout restart deploy
-----------------------
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
data:
  Corefile: |
    .:53 {
        errors
        health {
          lameduck 5s
        }
        ready
        log . {
          class error
        }
        kubernetes cluster.local in-addr.arpa ip6.arpa {
          pods insecure
          fallthrough in-addr.arpa ip6.arpa
        }
        prometheus :9153
        forward . 8.8.8.8 8.8.4.4
        reload
        loadbalance
    }
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"Corefile":".:53 {\n    errors\n    health {\n      lameduck 5s\n    }\n    ready\n    log . {\n      class error\n    }\n    kubernetes cluster.local in-addr.arpa ip6.arpa {\n      pods insecure\n      fallthrough in-addr.arpa ip6.arpa\n    }\n    prometheus :9153\n    forward . 8.8.8.8 8.8.4.4    **reload**\n    loadbalance\n}\n"},"kind":"ConfigMap","metadata":{"annotations":{},"creationTimestamp":"2022-01-25T09:35:32Z","labels":{"addonmanager.kubernetes.io/mode":"EnsureExists","k8s-app":"kube-dns"},"managedFields":[{"apiVersion":"v1","fieldsType":"FieldsV1","fieldsV1":{"f:data":{".":{},"f:Corefile":{}},"f:metadata":{"f:annotations":{".":{},"f:kubectl.kubernetes.io/last-applied-configuration":{}},"f:labels":{".":{},"f:addonmanager.kubernetes.io/mode":{},"f:k8s-app":{}}}},"manager":"kubectl-client-side-apply","operation":"Update","time":"2022-01-25T09:35:32Z"}],"name":"coredns","namespace":"kube-system","resourceVersion":"518","selfLink":"/api/v1/namespaces/kube-system/configmaps/coredns","uid":"8a73848a-4118-417b-80f5-2175bb64acc4"}}
  creationTimestamp: "2022-01-25T09:35:32Z"
  labels:
    addonmanager.kubernetes.io/mode: EnsureExists
    k8s-app: kube-dns
  name: coredns
  namespace: kube-system
  resourceVersion: "18764"
  selfLink: /api/v1/namespaces/kube-system/configmaps/coredns
  uid: 8a73848a-4118-417b-80f5-2175bb64acc4
~                                                                                                                                                            ~                                                                                                                                                            ~                                                                                                                                                            ~                                             
sudo microk8s.kubectl -n kube-system edit configmaps coredns -o yaml
sudo microk8s.kubectl -n kube-system rollout restart deploy

MVVM WPF Add new item to the DataGrid from Textboxes which are bound to the nested properties of the Model class

copy iconCopydownload iconDownload
<UserControl.Resources>
    <ControlTemplate x:Key="RoundedTextBoxCt">
        <Border 
            Width="{TemplateBinding Width}"                    
            Height="{TemplateBinding Height}"                                    
            BorderBrush="{TemplateBinding BorderBrush}"              
            BorderThickness="{TemplateBinding BorderThickness}"          
            Background="{TemplateBinding Background}"          
            x:Name="Bd" 
            CornerRadius="5">
            <ScrollViewer
                Padding="{TemplateBinding Padding}"                        
                VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" 
                Foreground="{TemplateBinding Foreground}"
                x:Name="PART_ContentHost"/>
        </Border>
    </ControlTemplate>
    
    <Style x:Key="ParameterTextBox" TargetType="TextBox">
        <Setter Property="Width"                    Value="180"/>
        <Setter Property="Height"                   Value="35"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding"                  Value="5"/>
        <Setter Property="Foreground"               Value="Black"/>
        <Setter Property="BorderBrush"              Value="#EAEAEA"/>
        <Setter Property="BorderThickness"          Value="3"/>
        <Setter Property="Background"               Value="White"/>
        <Setter Property="Template" Value="{StaticResource RoundedTextBoxCt}"/>
    </Style>
    
    ...
    
</UserControl.Resources>

Firestore 'await addDoc' (Web version 9) hangs forever when network connection is lost

copy iconCopydownload iconDownload
import { collection, doc, setDoc } from "firebase/firestore"; 

// Add a new document with a generated id
const newCityRef = doc(collection(db, "cities"));

// later...
await setDoc(newCityRef, data);

index.esm2017.js:370 Uncaught (in promise) FirebaseError: Missing or insufficient permissions in ReactJs

copy iconCopydownload iconDownload
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /series/{document} {
      allow read, write: if true;
    }
  }
}

Get the position where drawn text is truncated by GraphicsPath.DrawString

copy iconCopydownload iconDownload
Private Sub FillTextSolid(g As Graphics, bounds As RectangleF, text As String, font As Font, fillColor As Color)
    Using gp As GraphicsPath = New GraphicsPath(),
        brush As New SolidBrush(fillColor),
        format = New StringFormat(StringFormat.GenericTypographic)
        format.Trimming = StringTrimming.EllipsisWord
        gp.AddString(text, font.FontFamily, font.Style, font.Size, bounds, StringFormat.GenericTypographic)
        g.FillPath(brush, gp)
        Dim lastCharPosition = GetPathLastCharPosition(g, format, gp, bounds, text, font)
    End Using
End Sub
Dim [Last Char Position] = GetPathLastCharPosition(
    [Graphics], 
    [StringFormat], 
    [GraphicsPath], 
    [RectangleF], 
    [String], 
    [Font]
)
Private Function GetPathLastCharPosition(g As Graphics, format As StringFormat, path As GraphicsPath, bounds As RectangleF, text As String, font As Font) As Integer
    Dim textLength As Integer = text.Length
    Dim p = path.GetLastPoint()
    bounds.Height += font.Height

    For charPos As Integer = 0 To text.Length - 1 Step 32
        Dim count As Integer = Math.Min(textLength - charPos, 32)
        Dim charRanges = Enumerable.Range(charPos, count).Select(Function(c) New CharacterRange(c, 1)).ToArray()

        format.SetMeasurableCharacterRanges(charRanges)
        Dim regions As Region() = g.MeasureCharacterRanges(text, font, bounds, format)

        For r As Integer = 0 To regions.Length - 1
            If regions(r).IsVisible(p.X, p.Y) Then
                Return charRanges(r).First
            End If
        Next
    Next
    Return -1
End Function
private int GetPathLastCharPosition(Graphics g, StringFormat format, GraphicsPath path, RectangleF bounds, string text, Font font)
{
    int textLength = text.Length;
    var p = path.GetLastPoint();
    bounds.Height += font.Height;

    for (int charPos = 0; charPos < text.Length; charPos += 32) {
        int count = Math.Min(textLength - charPos, 32);
        var charRanges = Enumerable.Range(charPos, count).Select(c => new CharacterRange(c, 1)).ToArray();

        format.SetMeasurableCharacterRanges(charRanges);
        Region[] regions = g.MeasureCharacterRanges(text, font, bounds, format);

        for (int r = 0; r < regions.Length; r++) {
            if (regions[r].IsVisible(p.X, p.Y)) {
                return charRanges[r].First;
            }
        }
    }
    return -1;
}
-----------------------
Private Sub FillTextSolid(g As Graphics, bounds As RectangleF, text As String, font As Font, fillColor As Color)
    Using gp As GraphicsPath = New GraphicsPath(),
        brush As New SolidBrush(fillColor),
        format = New StringFormat(StringFormat.GenericTypographic)
        format.Trimming = StringTrimming.EllipsisWord
        gp.AddString(text, font.FontFamily, font.Style, font.Size, bounds, StringFormat.GenericTypographic)
        g.FillPath(brush, gp)
        Dim lastCharPosition = GetPathLastCharPosition(g, format, gp, bounds, text, font)
    End Using
End Sub
Dim [Last Char Position] = GetPathLastCharPosition(
    [Graphics], 
    [StringFormat], 
    [GraphicsPath], 
    [RectangleF], 
    [String], 
    [Font]
)
Private Function GetPathLastCharPosition(g As Graphics, format As StringFormat, path As GraphicsPath, bounds As RectangleF, text As String, font As Font) As Integer
    Dim textLength As Integer = text.Length
    Dim p = path.GetLastPoint()
    bounds.Height += font.Height

    For charPos As Integer = 0 To text.Length - 1 Step 32
        Dim count As Integer = Math.Min(textLength - charPos, 32)
        Dim charRanges = Enumerable.Range(charPos, count).Select(Function(c) New CharacterRange(c, 1)).ToArray()

        format.SetMeasurableCharacterRanges(charRanges)
        Dim regions As Region() = g.MeasureCharacterRanges(text, font, bounds, format)

        For r As Integer = 0 To regions.Length - 1
            If regions(r).IsVisible(p.X, p.Y) Then
                Return charRanges(r).First
            End If
        Next
    Next
    Return -1
End Function
private int GetPathLastCharPosition(Graphics g, StringFormat format, GraphicsPath path, RectangleF bounds, string text, Font font)
{
    int textLength = text.Length;
    var p = path.GetLastPoint();
    bounds.Height += font.Height;

    for (int charPos = 0; charPos < text.Length; charPos += 32) {
        int count = Math.Min(textLength - charPos, 32);
        var charRanges = Enumerable.Range(charPos, count).Select(c => new CharacterRange(c, 1)).ToArray();

        format.SetMeasurableCharacterRanges(charRanges);
        Region[] regions = g.MeasureCharacterRanges(text, font, bounds, format);

        for (int r = 0; r < regions.Length; r++) {
            if (regions[r].IsVisible(p.X, p.Y)) {
                return charRanges[r].First;
            }
        }
    }
    return -1;
}
-----------------------
Private Sub FillTextSolid(g As Graphics, bounds As RectangleF, text As String, font As Font, fillColor As Color)
    Using gp As GraphicsPath = New GraphicsPath(),
        brush As New SolidBrush(fillColor),
        format = New StringFormat(StringFormat.GenericTypographic)
        format.Trimming = StringTrimming.EllipsisWord
        gp.AddString(text, font.FontFamily, font.Style, font.Size, bounds, StringFormat.GenericTypographic)
        g.FillPath(brush, gp)
        Dim lastCharPosition = GetPathLastCharPosition(g, format, gp, bounds, text, font)
    End Using
End Sub
Dim [Last Char Position] = GetPathLastCharPosition(
    [Graphics], 
    [StringFormat], 
    [GraphicsPath], 
    [RectangleF], 
    [String], 
    [Font]
)
Private Function GetPathLastCharPosition(g As Graphics, format As StringFormat, path As GraphicsPath, bounds As RectangleF, text As String, font As Font) As Integer
    Dim textLength As Integer = text.Length
    Dim p = path.GetLastPoint()
    bounds.Height += font.Height

    For charPos As Integer = 0 To text.Length - 1 Step 32
        Dim count As Integer = Math.Min(textLength - charPos, 32)
        Dim charRanges = Enumerable.Range(charPos, count).Select(Function(c) New CharacterRange(c, 1)).ToArray()

        format.SetMeasurableCharacterRanges(charRanges)
        Dim regions As Region() = g.MeasureCharacterRanges(text, font, bounds, format)

        For r As Integer = 0 To regions.Length - 1
            If regions(r).IsVisible(p.X, p.Y) Then
                Return charRanges(r).First
            End If
        Next
    Next
    Return -1
End Function
private int GetPathLastCharPosition(Graphics g, StringFormat format, GraphicsPath path, RectangleF bounds, string text, Font font)
{
    int textLength = text.Length;
    var p = path.GetLastPoint();
    bounds.Height += font.Height;

    for (int charPos = 0; charPos < text.Length; charPos += 32) {
        int count = Math.Min(textLength - charPos, 32);
        var charRanges = Enumerable.Range(charPos, count).Select(c => new CharacterRange(c, 1)).ToArray();

        format.SetMeasurableCharacterRanges(charRanges);
        Region[] regions = g.MeasureCharacterRanges(text, font, bounds, format);

        for (int r = 0; r < regions.Length; r++) {
            if (regions[r].IsVisible(p.X, p.Y)) {
                return charRanges[r].First;
            }
        }
    }
    return -1;
}
-----------------------
Private Sub FillTextSolid(g As Graphics, bounds As RectangleF, text As String, font As Font, fillColor As Color)
    Using gp As GraphicsPath = New GraphicsPath(),
        brush As New SolidBrush(fillColor),
        format = New StringFormat(StringFormat.GenericTypographic)
        format.Trimming = StringTrimming.EllipsisWord
        gp.AddString(text, font.FontFamily, font.Style, font.Size, bounds, StringFormat.GenericTypographic)
        g.FillPath(brush, gp)
        Dim lastCharPosition = GetPathLastCharPosition(g, format, gp, bounds, text, font)
    End Using
End Sub
Dim [Last Char Position] = GetPathLastCharPosition(
    [Graphics], 
    [StringFormat], 
    [GraphicsPath], 
    [RectangleF], 
    [String], 
    [Font]
)
Private Function GetPathLastCharPosition(g As Graphics, format As StringFormat, path As GraphicsPath, bounds As RectangleF, text As String, font As Font) As Integer
    Dim textLength As Integer = text.Length
    Dim p = path.GetLastPoint()
    bounds.Height += font.Height

    For charPos As Integer = 0 To text.Length - 1 Step 32
        Dim count As Integer = Math.Min(textLength - charPos, 32)
        Dim charRanges = Enumerable.Range(charPos, count).Select(Function(c) New CharacterRange(c, 1)).ToArray()

        format.SetMeasurableCharacterRanges(charRanges)
        Dim regions As Region() = g.MeasureCharacterRanges(text, font, bounds, format)

        For r As Integer = 0 To regions.Length - 1
            If regions(r).IsVisible(p.X, p.Y) Then
                Return charRanges(r).First
            End If
        Next
    Next
    Return -1
End Function
private int GetPathLastCharPosition(Graphics g, StringFormat format, GraphicsPath path, RectangleF bounds, string text, Font font)
{
    int textLength = text.Length;
    var p = path.GetLastPoint();
    bounds.Height += font.Height;

    for (int charPos = 0; charPos < text.Length; charPos += 32) {
        int count = Math.Min(textLength - charPos, 32);
        var charRanges = Enumerable.Range(charPos, count).Select(c => new CharacterRange(c, 1)).ToArray();

        format.SetMeasurableCharacterRanges(charRanges);
        Region[] regions = g.MeasureCharacterRanges(text, font, bounds, format);

        for (int r = 0; r < regions.Length; r++) {
            if (regions[r].IsVisible(p.X, p.Y)) {
                return charRanges[r].First;
            }
        }
    }
    return -1;
}

Community Discussions

Trending Discussions on zx
  • Slow code when fetching data from Coinmarketcap api
  • Error message caught by Axios is undefined in production, but works locally?
  • Checking different elements of a cell in dataframe separated by comma using Pandas
  • Keep only characters in between two hyphens from a dataframe cell which has comma separated list
  • RenderTexture transparency working in editor becomes solid color in build
  • Regex Match anything inside the string
  • How to use google/zx to execute ffmpeg shell in windows?
  • microk8s coredns CrashLoopBackOff
  • MVVM WPF Add new item to the DataGrid from Textboxes which are bound to the nested properties of the Model class
  • Firestore 'await addDoc' (Web version 9) hangs forever when network connection is lost
Trending Discussions on zx

QUESTION

Slow code when fetching data from Coinmarketcap api

Asked 2022-Apr-03 at 09:47

I have the below code that fetches data from Coinmarketcap api and sends me a telegram message when parameters are met. When I fetch 100 coins then the code works fine. But when I fetch 5000 coins the code is very slow. The schedule time with refresh api is not the time that I have code.

Can someone see why the code is slow with fetching data from the api with 5000 coins?


A good answer is insert httpx / asyncio in the code. (answer Pawel Rubin (thanks). Does someone know how i can insert asyncio into the code?


from tokens import cmc_token
import json
import re
import requests
from datetime import date
import datetime
from datetime import datetime as dt
from datetime import datetime
from datetime import datetime, timedelta
import schedule
import time
import pandas as pd

lijst = []
price_change2 = []
coinlist = []

def tg():
    token = 'xxxxxxxxxxx'

    def write_json(data, filename='response.json'):
        with open(filename, 'w') as f:
            json.dump(data, f, indent=4, ensure_ascii=False)

    url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
    params = {'start': '1', 'limit': '5000', 'convert': 'usd', 'sort': 'date_added','sort_dir': 'desc' }
    headers = {'X-CMC_PRO_API_KEY': cmc_token}

    d = requests.get(url, headers=headers, params=params).json()
    
    def send_message_two(text='bla-bla-bla'):
        url = f'https://api.telegram.org/bot{token}/sendMessage'
        payload = {'chat_id': xxxxxxx, 'text' : text}

        p = requests.post(url, json=payload)
        return p


    for x in d['data']:
        date_json1 = x['last_updated']
        new_date1 = datetime.fromisoformat(date_json1[:-1])
        new_date_plus1 = new_date1 + timedelta(hours=2)
        new_date_str1 = new_date_plus1.strftime('%Y-%m-%d %H:%M:%S')

        price_now = x['symbol'], new_date_str1, x['quote']['USD']['price'], x['platform']
        price_change2.append(price_now)

        Dataset = pd.DataFrame(price_change2)
        df_new = Dataset.rename(columns={0:'coin', 2:'price', 1:'last_updated', 3:'platform'})
        zx = df_new.sort_values(['coin','last_updated'])


        zx['change1'] = zx.groupby('coin')['price'].pct_change().mul(100)
        zx['change1'] = zx['change1'].fillna(0)

        js = zx.to_json(orient="table")
        parsed = json.loads(js)

        for bn in parsed['data']:
            rt = bn['coin'], bn['change1'], bn['last_updated'],bn['price'], bn['platform']

            if bn['change1'] is None:
                return 0


            if bn['change1'] > 35 and bn['change1'] < 100 and bn['coin'] not in coinlist:
                coinlist.append(bn['coin'])
                send_message_two(rt)
                send_message_two('15 min change 35% all time')


schedule.every(900).seconds.do(tg)

while True:
    schedule.run_pending()
    time.sleep(1)

def main():
    pass


if __name__ == '__main__':
    main()

ANSWER

Answered 2022-Apr-01 at 13:50

The code is making requests sequentially for every element in parsed['data'].

Consider running your code asynchronously with some HTTP client that supports asyncio, for example httpx, and use asyncio.gather to run your requests concurrently.

Consider the following example which makes 50 GET requests to google.com using requests and using httpx. Note that the async solution is significantly faster.

import asyncio
import time

import requests
import httpx


def test_sync():
    for _ in range(50):
        requests.get("https://google.com")


async def test_async():
    async with httpx.AsyncClient() as client:
        tasks = [client.get("https://google.com") for _ in range(50)]
        await asyncio.gather(*tasks)


if __name__ == "__main__":
    start = time.process_time()
    test_sync()
    print(f"sync test took {time.process_time() - start} s")

    start = time.process_time()
    asyncio.run(test_async())
    print(f"async test took {time.process_time() - start} s")


# sync test took 23.0833 s
# async test took 0.2662 s

Source https://stackoverflow.com/questions/71706509

Community Discussions, Code Snippets contain sources that include Stack Exchange Network

Vulnerabilities

No vulnerabilities reported

Install zx

Requirement: Node version >= 16.0.0.

Support

Write your scripts in a file with .mjs extension in order to be able to use await on top level. If you prefer the .js extension, wrap your scripts in something like void async function () {...}().

DOWNLOAD this Library from

Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
over 430 million Knowledge Items
Find more libraries
Reuse Solution Kits and Libraries Curated by Popular Use Cases

Save this library and start creating your kit

Explore Related Topics

Share this Page

share link
Consider Popular Runtime Evironment Libraries
Compare Runtime Evironment Libraries with Highest Support
Compare Runtime Evironment Libraries with Highest Quality
Compare Runtime Evironment Libraries with Highest Security
Compare Runtime Evironment Libraries with Permissive License
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
over 430 million Knowledge Items
Find more libraries
Reuse Solution Kits and Libraries Curated by Popular Use Cases

Save this library and start creating your kit

  • © 2022 Open Weaver Inc.