Sunday, 31 October 2021

How to add extra element in data view while using tpl

How to add extra element in data view while using tpl.

I am using tpl with itemselector. Now I have add some extra div in that which is not coming from the store. How to add that ?

Here is my data view

onPanelLoad : function( panel , width , height , eOpts){
    var mypanel = panel.query('#somepanel')[0],
        imageStore = Ext.create('Ext.data.Store', {
            id:'imagesStore',
            model: Ext.define('Image', {
                extend: 'Ext.data.Model',
                fields: [
                    { name:'src', type:'string' },
                    { name:'caption', type:'string' }
                ]
            }),
            data: [{
                src: 'http://www.sencha.com/img/20110215-feat-drawing.png',
                caption: 'Drawing & Charts'
            }, {
                src: 'http://www.sencha.com/img/20110215-feat-data.png',
                caption: 'Advanced Data'
            }, {
                src: 'http://www.sencha.com/img/20110215-feat-html5.png',
                caption: 'Overhauled Theme'
            }]
        });

        var imageTpl = new Ext.XTemplate(
        
/* My Try  
        '<tpl for=".">',
            '<div class="item box-wrap">',
                   
                '<div class="content-box app-category" style="width:160px; height:160px;">',
                    '<div class="content-item">',
                        '<img src="{src}" />',
                    '</div>', 
                '</div>',
            '</div>',
*/

         '<tpl for=".">',
             '<div style="margin-bottom: 10px;" class="thumb-wrap">',
                 '<img src="{src}" />',
                 '<br/><span>{caption}</span>',
             '</div>',
        '</tpl>'
    ); 

      if(mypanel)
          mypanel.add([{
              layout:'vbox',
              items:[{
                  xtype : 'dataview',
                  store: Ext.data.StoreManager.lookup('imagesStore'),
                  tpl: imageTpl,
                  itemSelector: 'div.thumb-wrap',
                  emptyText: 'No images available',
             }]
        }]);
    

This is what I am trying to add.

'<tpl for=".">',
    '<div class="item box-wrap">',
        '<div class="content-box app-category" style="width:160px; height:160px;">',
            '<div class="content-item">',
                '<img src="{src}" />',
            '</div>', 
        '</div>',
    '</div>',

When I am trying I am getting Uncaught TypeError: Cannot read property 'internalId' of undefined

Any work around for this?



from How to add extra element in data view while using tpl

Use onLocationChanged() updates inside a foreground service

I'm trying to create a foreground service that can track the user's location with the phone's GPS. I have a ForegroundService class, where in the onCreate() method I call a function that should start by asking for location updates:

fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)

locationManager = getSystemService(LOCATION_SERVICE) as LocationManager

val locationListener: LocationListener = this
locationManager.requestLocationUpdates(
    LocationManager.GPS_PROVIDER, 2000, 0F, locationListener
)

My class is implementing Service() and LocationListener. I then override onLocationChanged, where I should log the current speed (and leave onAccuracyChanged empty).

Of course I have a notification correctly showing in android, so the service is definitely running, but I seem to get no location updates anyways. What could the problem be? Thank you.



from Use onLocationChanged() updates inside a foreground service

Slack Slash commands error: failed with the error "operation_timeout"

I am using Slack Slash commands to send requests to my AWS Lambda python app.

However, after sending the command, Slack is returning the message failed with the error "operation_timeout":

operation_timeout

Even though I received this message, my request is sent successfully, and the job is done.

I just want to know how to get rid of this message sent by slack.

I know that Slack expects an HTTP POST 200 OK as acknowledgement response as from this link here but I did send one the moment the payload is received as shown here:

lambda_handler.py

def slack_acknowledgment_response(resonse_url):

    # Make API Call to Slack API
    requests.post(
        resonse_url, 
        {'text': 'Test'}
    )


# This is my main event
def main(event, context):

    payload = parse_qs(event['postBody'])

    response_url = payload['response_url'][0]

    slack_acknowledgment_response(response_url)

    time.sleep(5)

I intentionally added a sleep period of 5 seconds because I get this error if the runtime of my script takes longer than 3 seconds, even though I already sent an acknowledgement before 3 seconds.

Can someone please help out ?



from Slack Slash commands error: failed with the error "operation_timeout"

react-hook-form not working when onSubmit called

I'm trying to have react forms save and then update some user settings.

import React, { useState, useEffect } from "react";
import Dropdown from "components/Settings/Preferences/Dropdown";
import { useForm } from "react-hook-form";

function Pill({ value, handleChange,optionName,substanceKey,key }) {
    return (
      <div  className="bg-gray-600 text-white text-xs px-2 py-0.5 w-max-content rounded-lg align-middle mr-1 mb-1">
        {value}
        <svg
          xmlns="http://www.w3.org/2000/svg"
          width="16"
          height="16"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          strokeWidth="2"
          strokeLinecap="round"
          strokeLinejoin="round"
          className="inline-block align-middle cursor-pointer"
          onClick={() => handleChange(value, "remove",optionName,substanceKey)}
        >
          <line x1="18" y1="6" x2="6" y2="18" />
          <line x1="6" y1="6" x2="18" y2="18" />
        </svg>
      </div>
    );
  }

function InventoryPage(props) {
    const [substanceDetails, setSettings] = useState(
        [
          {
            substance: "Modafinil",
            scheduledtimes: [8, 13],
            startdate: "1996-12-02",
            enddate: "2020-01-01",
            duration: 2,
            planneddose: "10mg"
          },
          {
            substance: "Coveve",
            scheduledtimes: [8, 12],
            startdate: "1996-12-02",
            enddate: "2020-01-01",
            duration: 2,
            planneddose: "10mg"
          }
      );


      const { register, handleSubmit,watch, errors,formState } = useForm();
      const handleChange = (value, mode,optionName,substanceKey) => {
        var removedSubstances ;
        if(mode==="remove") {
          if (optionName === "substance" ) {
            removedSubstances = substanceDetails.find(v=> v.substance === value) ? substanceDetails.filter(v => v.substance !== value) : [...substanceDetails, value]
          }
         
       else {
          removedSubstances = substanceDetails.reduce((acc, key) => {
      //  return acc; // remove keys
        if (optionName === "scheduledtimes") { //works
          // remove 14 in times for keys
          return [...acc, { ...key,
            scheduledtimes: key.scheduledtimes.filter(time => time !== value)
          }]
        }
        if (optionName === "planneddose") {
          // modify the power by concatenating an new object with partial info
          if (key.substance == substanceKey){
      
          return [...acc, {...key,
            planneddose: null
          }];
        } else {
          return [...acc, {...key,
            planneddose: key.planneddose
          }];
        }
        }
      
        if (optionName === "startdate") {
          // modify the power by concatenating an new object with partial info
          if (key.substance == substanceKey){
      
          return [...acc, {...key,
            startdate: null
          }];
        } else {
          return [...acc, {...key,
            startdate: key.startdate
          }];
        }
        }
        if (optionName === "enddate") {
          // modify the power by concatenating an new object with partial info
          if (key.substance == substanceKey){
      
          return [...acc, {...key,
            enddate: null
          }];
        } else {
          return [...acc, {...key,
            enddate: key.enddate
          }];
        }
        }
      
        if (optionName === "duration") {
          // modify the power by concatenating an new object with partial info
          if (key.substance == substanceKey){
      
          return [...acc, {...key,
            duration: null
          }];
        } else {
          return [...acc, {...key,
            duration: key.duration
          }];
        }
        }
        
      }
      , []);
       }
      setSettings(removedSubstances)
        }
      };
  const onSubmit = data => console.log(data);
  const [pending, setPending] = useState(false);
  
   
  console.log(watch("example")); // watch input value by passing the name of it

  if (substanceDetails === false) {
    return (
      <div className="md:grid md:grid-cols-3 md:gap-6">
        <div className="md:col-span-1">
          <h3 className="text-lg font-medium leading-6 text-gray-900">
            Substances
          </h3>
        </div>
        <div className="mt-5 md:mt-0 md:col-span-2 font-mono font-medium text-blue-500">
          loading...
        </div>
      </div>
    );
  }
  

  return (
    <div className="md:grid md:grid-cols-3 md:gap-6">

    <div className="mt-5 md:mt-0 md:col-span-2">
     <form onSubmit={handleSubmit(onSubmit)} id="formName">
          <div className="flex flex-wrap mt-2">
            {substanceDetails &&
              substanceDetails.map((subst) => (
                <Pill
                  registerInput={register}
                  optionLabel="substance"
                  value={subst.substance}
                  key={subst.substance}
                  substanceKey = {subst.substance}
                  optionName={"substance"}
                  // allOptions={["Dexamphetamine", "Ritalin"]}
                  handleChange={handleChange}
                  error={formState.errors?.content ? true : false}
                />
              ))}
          </div>
          <Dropdown
            registerInput={register}
            optionLabel="Substance"
            selectedOption={substanceDetails.substance}
            optionName={"substance"}
            allOptions={["Dexamphetamine", "Ritalin"]}
            error={formState.errors?.content ? true : false}
          />
          <button
            className="inline-flex items-center justify-center px-5 py-2 border border-transparent text-base leading-6 font-medium rounded-md text-indigo-700 bg-indigo-100 hover:text-indigo-600 hover:bg-indigo-50 focus:outline-none focus:shadow-outline focus:border-indigo-300 transition duration-150 ease-in-out"
            variant={props.buttonColor}
            size={props.inputSize}
            type="submit"
            disabled={pending}
            form="formName"

          >
            {pending ? (
              <>
                <span>Saving</span>
                <svg
                  xmlns="http://www.w3.org/2000/svg"
                  viewBox="0 0 24 24"
                  className="animate-spin h-4 w-4 ml-3 fill-current"
                >
                  <path d="M0 11c.511-6.158 5.685-11 12-11s11.489 4.842 12 11h-2.009c-.506-5.046-4.793-9-9.991-9s-9.485 3.954-9.991 9h-2.009zm21.991 2c-.506 5.046-4.793 9-9.991 9s-9.485-3.954-9.991-9h-2.009c.511 6.158 5.685 11 12 11s11.489-4.842 12-11h-2.009z" />
                </svg>
              </>
            ) : (
              <span>Save</span>
            )}
          </button>
        </form>
    </div>
    </div>
  );
}

export default InventoryPage;

Currently when hitting submit, the form itself is just refreshing the page, and it's not using the onSubmit constant. I'm not sure what's going on, love some help :)

Sandbox link, code is compiling but probably easier to vet the code itself

https://codesandbox.io/s/using-tailwind-with-nextjs-forked-nnr0l?file=/pages/index.js



from react-hook-form not working when onSubmit called

Angular Uncaught (in promise): ChunkLoadError: Loading chunk XXXfailed

I am using angular 12, and I use Service Worker to deploy new versions.

It looks like each time I deploy a new version to production (and for some reason not on staging). Some user receive a bunch of error like

Uncaught (in promise): ChunkLoadError: Loading chunk XXX failed.

There is quite some post about this issue, but it appear to me the solution is case by case.

The difference I have in production compare to stg is just.

I call enableProdMode();

I have the service worker setup like this :

    ServiceWorkerModule.register('ngsw-worker.js', {
      enabled:
        environment.env === Environment.PRODUCTION || environment.env === Environment.STAGING,
      registrationStrategy: 'registerWhenStable:30000',
    }),

And My Config looks like

{
  "$schema": "../../node_modules/@angular/service-worker/config/schema.json",
  "index": "/index.html",
  "assetGroups": [
    {
      "name": "app",
      "installMode": "prefetch",
      "resources": {
        "files": ["/favicon.ico", "/index.html", "/manifest.webmanifest", "/*.css", "/*.js"]
      }
    },
    {
      "name": "assets",
      "installMode": "lazy",
      "updateMode": "lazy",
      "resources": {
        "files": ["/assets/**", "/*.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)"]
      }
    }
  ]
}

And this is how I show the updates (basically a banner that can be ignored) :

@Component({
  selector: 'app-live-update-notifier',
  templateUrl: './live-update-notifier.component.html',
  styleUrls: ['./live-update-notifier.component.scss'],
})
export class LiveUpdateNotifierComponent implements OnDestroy {
  hasUpdate = false;
  isIgnored = false;

  private subscription = new SubSink();

  constructor(
    @Inject(DOCUMENT) private document: Document,
    private swUpdate: SwUpdate,
    private appRef: ApplicationRef,
    private changeDetectorRef: ChangeDetectorRef
  ) {
    const appIsStable$ = this.appRef.isStable.pipe(first((isStable) => isStable === true));
    const everySixHours$ = interval(6 * 60 * 60 * 1000);
    const everySixHoursOnceAppIsStable$ = concat(appIsStable$, everySixHours$);

    if (swUpdate.isEnabled) {
      this.subscription.add(
        everySixHoursOnceAppIsStable$.subscribe(() => swUpdate.checkForUpdate()),
        this.swUpdate.available.subscribe((evt) => {
          if (evt.type === 'UPDATE_AVAILABLE') {
            this.hasUpdate = true;
            this.changeDetectorRef.detectChanges();
          }
        })
      );
    }
  }

  ngOnDestroy(): void {
    this.subscription.unsubscribe();
  }

  updateApp() {
    this.document.location.reload();
  }

  skipNotification() {
    this.isIgnored = true;
  }
}

If you have any clue that could guide to the solution, thank you.

EDIT :

I have a bunch of lazy loaded module all across my app. I thought the reason was that some module where not loaded previous to a new version, and when user navigate to them after deploy they would have this chunk problem. But I tried to do emulate this issue by

  • editing module A and B
  • deploy
  • navigate to module A
  • editing module A and B
  • deploy
  • navigate to old module B from old A, so without updating the version of the APP RES : old B still load normally

so I don't think it s this issue



from Angular Uncaught (in promise): ChunkLoadError: Loading chunk XXXfailed

pyinstaller kivy error Unable to get any Image provider, abort

hi i create a kivy python with pyinstaller in virtualenv

my program run in console with command python cipol.py is run successfully wihout no error but when create exe with pyinstaller with command pyinstaller cipol.spec

it will return

[INFO ] [Factory ] 186 symbols loaded

[INFO ] [Image ] Providers: (img_tex, img_dds, img_ffpyplayer, img_pil, img_sdl2 ignored)

[CRITICAL] [App ] Unable to get any Image provider, abort.

this is my spec file:

# -*- mode: python ; coding: utf-8 -*-
from kivy_deps import sdl2, glew
import os
os.environ['KIVY_IMAGE'] = 'pil,sdl2'
from importlib import __import__

block_cipher = None


a = Analysis(['C:\\Users\\user\\Desktop\\cipol-kivy\\exe\\venv\\Lib\\sync\\cipol.py'],
         pathex=['C:\\Users\\user\\Desktop\\cipol-kivy\\exe\\venv\\Lib\\sync'],
         binaries=[],
         datas=[],
         hiddenimports=['win32file','win32timezone'],
         hookspath=[],
         runtime_hooks=[],
         excludes=[],
         win_no_prefer_redirects=False,
         win_private_assemblies=False,
         cipher=block_cipher,
         noarchive=False)
a.datas += [('logo.png','C:\\Users\\user\\Desktop\\cipol-kivy\\exe\\venv\\Lib\\sync\\logo.png','DATA'), ('style.kv', 'C:\\Users\\user\\Desktop\\cipol-kivy\\exe\\venv\\Lib\\sync\\style.kv', 'DATA')]

pyz = PYZ(a.pure, a.zipped_data,
         cipher=block_cipher)
exe = EXE(pyz,
      a.scripts,
      [],
      exclude_binaries=True,
      name='cipolsync',
      debug=True,
      bootloader_ignore_signals=False,
      strip=False,
      upx=True,
      console=True,
      icon='C:\\Users\\user\\Desktop\\cipol-kivy\\exe\\venv\\Lib\\sync\\cipol.ico')
coll = COLLECT(exe,
            Tree('C:\\Users\\user\\Desktop\\cipol-kivy\\exe\\venv\\Lib\\sync\\'),
           a.binaries,
           a.zipfiles,
           a.datas,
           *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
           strip=False,
           upx=True,
           upx_exclude=[],
           name='cipolsync')


from pyinstaller kivy error Unable to get any Image provider, abort

Find all possible combinations of strings that match a given pattern in JS

So I have a dictionary where each key is mapped to an array of letters:

tCategories = { "T": ["t","d","th"],
                "P": ["p","t","k","q"],
                "N": ["m","n"] };

And an input string that contains a handful of patterns delimited by commas, e.g. "aT,Ps,eNe,NP", where a substring that is a valid key of tCategories acts a stand-in for any of the letters in tCategories[key].

What I'm trying to figure out is how to find every combination of each pattern listed in the input string and put them all in an array. So e.g. the expected output for foo("aT,Ps,eNe,NP") would be ["at","ad","ath","ps","ts","ks","qs","eme","ene","mp","mt","mk","mq","np","nt","nk","nq"].

My first instinct would either be to call String.split(",") on the input string to deal with each substring separately, or else iterate via for (var key in tCategories) { input.replace(new RegExp(key, "g"), "["+tCategories[key].join("|")+"]" }, or something... but I just can't seem to find a useful pathway between those and the expected output. It would involve... what, basically implementing the distributive property but for letters instead of numbers? How do I do this?



from Find all possible combinations of strings that match a given pattern in JS

Flutter - how to scroll to the bottom of a listview?

I use this code to scroll:

WidgetsBinding.instance?.addPostFrameCallback((_) => _scrollToEnd());

_scrollToEnd() method is:

_scrollController.animateTo(
  _scrollController.position.maxScrollExtent,
  duration: const Duration(
    milliseconds: 200,
  ),
  curve: Curves.easeInOut,
);

Imagine this as a normal chat screen. It scrolls to the bare bottom if the messages are in 1 line. But as soon as a message gets to 2+ lines it struggles to scroll to the bare bottom. The more rows of a message the less it scrolls to the bottom.

This is how it looks like when i enter the chat:

But if i scroll down further this is the bottom of the chat:

I noticed there's also a case when:

  1. I enter the chat.
  2. It scrolls down like on the first image.
  3. If i tap anywhere on the screen, it continues to scroll to the bare bottom of the listview like on the second image.

Why does this happen and how do i fix this?



from Flutter - how to scroll to the bottom of a listview?

DCGAN understanding generator update step

Here is some DCGAN example in Pytorch: https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html#training

I wonder why we don't zero discriminator gradients before we update generator? (I added line in original code # netD.zero_grad() # Q: why we don't do this?) Is it because gradients accumulate in some separate variable and don't affect gradients calculation in generator and only can affect update of weights, but on generator update step we only update generator weights so it's fine?

Here is code section from training loop:

############################
# (1) Update D network: maximize log(D(x)) + log(1 - D(G(z)))
###########################
## Train with all-real batch
netD.zero_grad()
# Format batch
real_cpu = data[0].to(device)
b_size = real_cpu.size(0)
label = torch.full((b_size,), real_label, dtype=torch.float, device=device)
# Forward pass real batch through D
output = netD(real_cpu).view(-1)
# Calculate loss on all-real batch
errD_real = criterion(output, label)
# Calculate gradients for D in backward pass
errD_real.backward()
D_x = output.mean().item()

## Train with all-fake batch
# Generate batch of latent vectors
noise = torch.randn(b_size, nz, 1, 1, device=device)
# Generate fake image batch with G
fake = netG(noise)
label.fill_(fake_label)
# Classify all fake batch with D
output = netD(fake.detach()).view(-1)
# Calculate D's loss on the all-fake batch
errD_fake = criterion(output, label)
# Calculate the gradients for this batch, accumulated (summed) with previous gradients
errD_fake.backward()
D_G_z1 = output.mean().item()
# Compute error of D as sum over the fake and the real batches
errD = errD_real + errD_fake
# Update D
optimizerD.step()

############################
# (2) Update G network: maximize log(D(G(z)))
###########################
netG.zero_grad()
# netD.zero_grad() # Q: why we don't do this?
label.fill_(real_label)  # fake labels are real for generator cost
# Since we just updated D, perform another forward pass of all-fake batch through D
output = netD(fake).view(-1)
# Calculate G's loss based on this output
errG = criterion(output, label)
# Calculate gradients for G
errG.backward()
D_G_z2 = output.mean().item()
# Update G
optimizerG.step()


from DCGAN understanding generator update step

AssetLinks file expected traffic and failure scenarios

I am working on setting up my Android app to work with HTTP Web Links according to the various guides and protocols and have successfully setup the deeplinks which includes serving my assetlinks file from /.well-known/assetlinks.json. Everything is working as expected. What I am struggling with from an operational perspective is how to go about maintaining this file -- specifically during update and failure scenarios.

Let's say I have existing customers who have downloaded my app and are using weblinks just fine. I update my assetlink file and push the new version to my server. When do my customers get the updated assetlink file? Is the OS configured to check for updates on some cadence or app launch? Is it only on app update or reinstall?

Similarly, imagine my website is down. New users are installing my app and the OS will not be able to associate my domain and when customers click the HTTP web links they will not be deeplinked into the app. This makes sense. But after I recover from my outage when will the customer get their assetlink file given the app is already installed?

Similarly lets say I upload an invalid assetlinks.json file. Will this break the current web links for existing customers who already had a valid association when they first installed the app?

Understanding these issues will ultimately help me better troubleshoot customer issues and tune the expected traffic I should expect to see for my assetlinks file from my server.



from AssetLinks file expected traffic and failure scenarios

Javascript find intersections given two functions

I am trying today to solve for the coordinates where functions intersect. I am using the nerdarmer library right now, but it only returns only one solution out of all the possible solutions. For example, I want the code below to print -1, 0, 1 but it only outputs 0. Another example is if I want to find intersections between y = 0 and sin(x), I want the output to be ..., (-2pi, 0), (-pi, 0), (pi, 0), (2pi, 0), (3pi, 0), ...

intersect("x^3", "x")
function intersect(f1, f2){
    var x = nerdamer.solve('f1', 'f2');
    console.log(x.toString());  
}

Is there any way to get all the possible solutions?



from Javascript find intersections given two functions

Saturday, 30 October 2021

How can I have a synchronous facade over asyncpg APIs with Python asyncio?

Imagine an asynchronous aiohttp web application that is supported by a Postgresql database connected via asyncpg and does no other I/O. How can I have a middle-layer hosting the application logic, that is not async? (I know I can simply make everything async -- but imagine my app to have massive application logic, only bound by database I/O, and I cannot touch everything of it).

Pseudo code:

async def handler(request):
    # call into layers over layers of application code, that simply emits SQL
    ...

def application_logic():
    ...
    # This doesn't work, obviously, as await is a syntax
    # error inside synchronous code.
    data = await asyncpg_conn.execute("SQL")
    ...
    # What I want is this:
    data = asyncpg_facade.execute("SQL")
    ...

How can a synchronous façade over asyncpg be built, that allows the application logic to make database calls? The recipes floating around like using async.run() or asyncio.run_coroutine_threadsafe() etc. do not work in this case, as we're coming from an already asynchronous context. I'd assume this cannot be impossible, as there already is an event loop that could in principle run the asyncpg coroutine.

Bonus question: what is the design rationale of making await inside sync a syntax error? Wouldn't it be pretty useful to allow await from any context that originated from a coroutine, so we'd have simple means to decompose an application in functional building blocks?

EDIT Extra bonus: beyond Paul's very good answer, that stays inside the "safe zone", I'd be interested in solutions that avoid blocking the main thread (leading to something more gevent-ish). See also my comment on Paul's answer ...



from How can I have a synchronous facade over asyncpg APIs with Python asyncio?

Error building Plotly Dash (npm run build)

I really want to contribute to Plotly Dash, following the contribution steps on (https://github.com/plotly/dash) and have an issue while building it on Windows 10/Python3.8.

Here are the commands what I ran during the process.

   1 python -m venv .venv/dev
   2 .venv\dev\scripts\activate
   3 pip install -e .[testing,dev]
   4 npm install
   5 npm run build

5th step showed me the following error.

> npm run build

> dash@ build E:\python\dash
> run-s private::build.*


> dash@ private::build.components E:\python\dash
> python dash/development/update_components.py 'all'

lerna notice cli v4.0.0
lerna info versioning independent
lerna notice filter including "*@('all')*"
lerna info filter [ "*@('all')*" ]
lerna ERR! EFILTER No packages remain after filtering [ "*@('all')*" ]

Failed installing npm dependencies for the following component packages 'all' (status=1)
lerna notice cli v4.0.0
lerna info versioning independent
lerna notice filter including "*@('all')*"
lerna info filter [ "*@('all')*" ]
lerna ERR! EFILTER No packages remain after filtering [ "*@('all')*" ]

🟢 Finished updating the following component packages 'all' (status=1) 🟢
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! dash@ private::build.components: `python dash/development/update_components.py 'all'`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the dash@ private::build.components script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\A\AppData\Roaming\npm-cache\_logs\2021-10-28T03_50_37_219Z-debug.log
ERROR: "private::build.components" exited with 1.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! dash@ build: `run-s private::build.*`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the dash@ build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\A\AppData\Roaming\npm-cache\_logs\2021-10-28T03_50_37_305Z-debug.log

Seems the error comes from the update_components.py, but not sure what's wrong.

>python dash/development/update_components.py all

'dash-html-components' is not recognized as an internal or external command, operable program or batch file.

Failed installing npm dependencies for the following component packages dash-core-components|dash-html-components|dash-table (status=255) 'dash-html-components' is not recognized as an internal or external command, operable program or batch file.

🟢 Finished updating the following component packages dash-core-components|dash-html-components|dash -table (status=255)

Any help or advice would be appreciated, Thanks.



from Error building Plotly Dash (npm run build)

How to render SVG image to PNG file in Python?

So I want to render SVG from python code having target resolution WxH (having SVG text as str, like this that I generate dynamically):

<svg width="200" height="200" viewBox="0 0 220 220"
     xmlns="http://www.w3.org/2000/svg">
  <filter id="displacementFilter">
    <feTurbulence type="turbulence" baseFrequency="0.05"
        numOctaves="2" result="turbulence"/>
    <feDisplacementMap in2="turbulence" in="SourceGraphic"
        scale="50" xChannelSelector="R" yChannelSelector="G"/>
  </filter>

  <circle cx="100" cy="100" r="100"
      style="filter: url(#displacementFilter)"/>
</svg>

into a png image. How to do such a thing in Python?



from How to render SVG image to PNG file in Python?

Redux-saga-test-plan expectSaga seems to retain state between independent tests

I have the below two tests

import {put, select, takeEvery} from 'redux-saga/effects';
import {combineReducers} from 'redux';

export default class SessionReducer {
    public static readonly _initialState: any = {
        disconnectCounts: {},
    };

    public static reducer(state: any = SessionReducer._initialState, action: any): any {
        // console.log('reducer', action);
        let newState: any;
        switch (action.type) {
            case 'DEVICE_DISCONNECTED':
                newState = {
                    ...state,
                };
                if (!newState.disconnectCounts[action.value]) newState.disconnectCounts[action.value] = 0;
                newState.disconnectCounts[action.value]++;
                newState.error = {
                    type: 'DEVICE_DISCONNECTED',
                    utc: 1,
                };

                return newState;
            default:
                return state;
        }
    }
}

export function* errorHandler() {
    yield takeEvery(['DEVICE_DISCONNECTED'], function* (action: any) {
        let state = yield select();
        console.log('*********', state);
        if (state.session.disconnectCounts[action.value] > 1) {
            yield put({
                type: 'WATCH_REBOOT_REQUEST',
            });
            // state.session.disconnectCounts[action.value] = 0
        }
    });
}
let action = {type: 'DEVICE_DISCONNECTED', value: '111'};
describe('Handles Error States and Transitions', () => {
    test('Sends watch reboot request when disconnection count threshold met', () => {
        return expectSaga(errorHandler)
            .withReducer(
                combineReducers({
                    session: SessionReducer.reducer,
                }),
                {session: SessionReducer._initialState},
            )
            .dispatch(action)
            .dispatch(action)
            .put({type: 'WATCH_REBOOT_REQUEST'})
            .run()
            .then((result: {storeState: any}) => {
                debugger;

                let session = result.storeState.session;
                expect(session.disconnectCounts[action.value]).toBe(2); // values for error are tested in reducer test
                expect(session.error).toBeTruthy(); // values for error are tested in reducer test
            });
    });
    test('Does not send WATCH_REBOOT_REQUEST when threshold not met', () => {
        return expectSaga(errorHandler)
            .withReducer(
                combineReducers({
                    session: SessionReducer.reducer,
                }),
                {session: SessionReducer._initialState},
            )
            .dispatch(action)
            .run()
            .then((result: {storeState: any}) => {
                let session = result.storeState.session;
                expect(session.disconnectCounts[action.value]).toBe(1); // values for error are tested in reducer test
                // expect(session.currentScreen).toEqual('actionRequiredIdleScreen');
            });
    });
});

If you run each test independently, i used .only, they pass but run them without .only and the second test always fails w/ too many values in disconnectCounts

  Handles Error States and Transitions
    ✓ Sends watch reboot request when disconnection count threshold met (263 ms)
    ✕ Does not send WATCH_REBOOT_REQUEST when threshold not met (258 ms)

  ● Handles Error States and Transitions › Does not send WATCH_REBOOT_REQUEST when threshold not met

    expect(received).toBe(expected) // Object.is equality

    Expected: 1
    Received: 3

      76 |             .then((result: {storeState: any}) => {
      77 |                 let session = result.storeState.session;
    > 78 |                 expect(session.disconnectCounts[action.value]).toBe(1); // values for error are tested in reducer test
         |                                                                ^
      79 |                 // expect(session.currentScreen).toEqual('actionRequiredIdleScreen');
      80 |             });
      81 |     });

      at __tests__/sagas/sagaStateIssue.ts:78:64
      at tryCallOne (node_modules/promise/lib/core.js:37:12)
      at node_modules/promise/lib/core.js:123:15
      at flush (node_modules/asap/raw.js:50:29)

What o what am I missing?



from Redux-saga-test-plan expectSaga seems to retain state between independent tests

How to display a heatmap on a specific parameter with geopandas?

In my very simple case I would like to display the heatmap of the points in the points GeoJSON file but not on the geographic density (lat, long). In the points file each point has a confidence property (a value from 0 to 1), how to display the heatmap on this parameter? weight=points.confidence don't seem to work.

for exemple:

#points.geojson

{ "type": "Feature", "properties": { "label": "triangle", "confidence": 0.67000001668930054, "longitude": 38.703471404215918, "latitude": 25.541625492300206, "area": 345.31, "id": 0, "icon": "play" }, "geometry": { "type": "Point", "coordinates": [ 38.703471404215918, 25.541625492300192 ] } },
...

The image below shows my result but it is on the geographic density not confidence score density.

import geoplot as gplt
import geopandas as gpd
import geoplot.crs as gcrs
import matplotlib.pyplot as plt

points = gpd.read_file('points.geojson')
polygons = gpd.read_file('polygons.geojson')

ax = gplt.polyplot(polygons, projection=gcrs.AlbersEqualArea(), zorder=1)
gplt.kdeplot(points, cmap='Reds', shade=True, clip=polygons, ax=ax) 
#weight=points.confidence don’t work inside kdeplot()

plt.show()

enter image description here



from How to display a heatmap on a specific parameter with geopandas?

Robolectric + JaCoCo | Issue while running unit test and not generating code coverage

I have added Robolectric to get support of Shadow APIs for writing unit tests. Able to get coverage percentage through default Intellij code coverage tool in Android Studio.

Now trying to add JaCoCo to get enhanced coverage report.

I'm using file build.gradle(app) for putting
plugin as apply from: "$rootDir/jacoco.gradle" and
dependency as testImplementation 'org.robolectric:robolectric:4.3.1'


jacoco.gradle

apply plugin: 'jacoco'

/*jacoco {
    // https://bintray.com/bintray/jcenter/org.jacoco:org.jacoco.core
    toolVersion = "0.7.1.201405082137"
}*/

android {
    testOptions {

        unitTests {
            includeAndroidResources = true
        }

        unitTests.all {
            systemProperty 'user.home', System.getenv('HOME')
            jacoco {
                includeNoLocationClasses = true
            }
        }

        unitTests {
            returnDefaultValues = true
        }
    }
}

project.afterEvaluate {

    android.applicationVariants.all { variant ->
        def name = variant.name
        def testTaskName = "test${name.capitalize()}UnitTest"

        tasks.create(name: "${testTaskName}Coverage", type: JacocoReport, dependsOn: "$testTaskName") {
            group = "Reporting"
            description = "Generate Jacoco coverage reports for the ${name.capitalize()} build."

            classDirectories.from = fileTree(
                    dir: "${project.buildDir}/intermediates/classes/${name}",
                    excludes: ['**/R.class',
                               '**/R$*.class',
                               '**/*$ViewInjector*.*',
                               '**/*$ViewBinder*.*',
                               '**/BuildConfig.*',
                               '**/Manifest*.*']
            )

            sourceDirectories.from = files(['src/main/java'].plus(android.sourceSets[name].java.srcDirs))
            executionData.from = files("${project.buildDir}/jacoco/${testTaskName}.exec")

            reports {
                xml.enabled = true
                html.enabled = true
            }
        }
    }
}

While running test cases through CL/Run(Button) or Jenkins, getting same error

com.example.LoginActivityTest > initializationError FAILED
    java.lang.RuntimeException at AndroidJUnit4.java:121
        Caused by: java.lang.reflect.InvocationTargetException at null:-1
            Caused by: java.lang.NoSuchMethodError at ShadowProviders.java:25

com.example.HomeActivityTest > initializationError FAILED
    java.lang.RuntimeException at AndroidJUnit4.java:121
        Caused by: java.lang.reflect.InvocationTargetException at null:-1
            Caused by: java.lang.NoSuchMethodError at ShadowProviders.java:25

I appreciate your help!



from Robolectric + JaCoCo | Issue while running unit test and not generating code coverage

How to make labels appear when clicking on a step plot subplot point in matplotlib?

I'm using matplotlib to make step graphs based on a dataframe, but I want one of the key/value of the dataframe to appear (signals_df['Gage']), instead of coordinates as annotation, but I always get the error: AttributeError: 'Line2D' object has no attribute 'get_offsets' when I click on the first subplot from bottom to top and the annotation does not appear. In fact, I commented out the annot.set_visible(False)and replaced the "" of the examples with val_gage, so that it will look like I want the annotation to appear one by one, when clicking on some point within the subplots. This is the code in question:

import pandas as pd
import numpy as np
import matplotlib as mtpl
from matplotlib import pyplot as plt
import matplotlib.ticker as ticker

annot = mtpl.text.Annotation

data = {
    # 'Name': ['Status', 'Status', 'HMI', 'Allst', 'Drvr', 'CurrTUBand', 'RUSource', 'RUReqstrPriority', 'RUReqstrSystem', 'RUResReqstStat', 'CurrTUBand', 'DSP', 'SetDSP', 'SetDSP', 'DSP', 'RUSource', 'RUReqstrPriority', 'RUReqstrSystem', 'RUResReqstStat', 'Status', 'Delay', 'Status', 'Delay', 'HMI', 'Status', 'Status', 'HMI', 'DSP'],
    # 'Value': [4, 4, 2, 1, 1, 1, 0, 7, 0, 4, 1, 1, 3, 0, 3, 0, 7, 0, 4, 1, 0, 1, 0, 1, 4, 4, 2, 3],
    # 'Gage': ['H1', 'H3', 'H3', 'H3', 'H3', 'H3', 'H3', 'H3', 'H3', 'H3', 'H3', 'H3', 'H3', 'H3', 'H3', 'H3', 'H3', 'H3', 'H3', 'H1', 'H1', 'H3', 'H3', 'H3', 'H1', 'H3', 'H3', 'H3'],
    # 'Id_Par': [0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 0, 0, 22, 22, 28, 28, 28, 28, 0, 0, 38, 38, 0, 0, 0, 0, 0]
    'Name': ['Lamp_D_Rq', 'Status', 'Status', 'HMI', 'Lck_D_RqDrv3', 'Lck_D_RqDrv3', 'Lck_D_RqDrv3', 'Lck_D_RqDrv3', 'Lamp_D_Rq', 'Lamp_D_Rq', 'Lamp_D_Rq', 'Lamp_D_Rq'],
    'Value': [0, 4, 4, 2, 1, 1, 2, 2, 1, 1, 3, 3],
    'Gage': ['F1', 'H1', 'H3', 'H3', 'H3', 'F1', 'H3', 'F1', 'F1', 'H3', 'F1', 'H3'],
    'Id_Par': [0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0]
    }

signals_df = pd.DataFrame(data)


def plot_signals(signals_df):
    print(signals_df)
    # Count signals by parallel
    signals_df['Count'] = signals_df.groupby('Id_Par').cumcount().add(1).mask(signals_df['Id_Par'].eq(0), 0)
    # Subtract Parallel values from the index column
    signals_df['Sub'] = signals_df.index - signals_df['Count']
    id_par_prev = signals_df['Id_Par'].unique()
    id_par = np.delete(id_par_prev, 0)
    signals_df['Prev'] = [1 if x in id_par else 0 for x in signals_df['Id_Par']]
    signals_df['Final'] = signals_df['Prev'] + signals_df['Sub']
    # Convert and set Subtract to index
    signals_df.set_index('Final', inplace=True)

    # Get individual names and variables for the chart
    names_list = [name for name in signals_df['Name'].unique()]
    num_names_list = len(names_list)
    num_axisx = len(signals_df["Name"])

    # Matplotlib's categorical feature to convert x-axis values to string
    x_values = [-1, ]
    x_values += (list(set(signals_df.index)))
    x_values = [str(i) for i in sorted(x_values)]

    # Creation Graphics
    fig, ax = plt.subplots(nrows=num_names_list, figsize=(10, 10), sharex=True)
    plt.xticks(np.arange(0, num_axisx), color='SteelBlue', fontweight='bold')

    # Loop to build the different graphs
    for pos, name in enumerate(names_list):
        # Creating a dummy plot and then remove it
        dummy, = ax[pos].plot(x_values, np.zeros_like(x_values))
        dummy.remove()

        # Get names by values and gage data
        data = signals_df[signals_df["Name"] == name]["Value"]
        data_gage = signals_df[signals_df["Name"] == name]["Gage"]

        # Get values axis-x and axis-y
        x_ = np.hstack([-1, data.index.values, len(signals_df) - 1])
        y_ = np.hstack([0, data.values, data.iloc[-1]])
        y_gage = np.hstack(["", "-", data_gage.values])
        # print(y_gage)

        # Plotting the data by position
        steps = ax[pos].plot(x_.astype('str'), y_, drawstyle='steps-post', marker='*', markersize=8, color='k', linewidth=2)
        ax[pos].set_ylabel(name, fontsize=8, fontweight='bold', color='SteelBlue', rotation=30, labelpad=35)
        ax[pos].yaxis.set_major_formatter(ticker.FormatStrFormatter('%0.1f'))
        ax[pos].yaxis.set_tick_params(labelsize=6)
        ax[pos].grid(alpha=0.4, color='SteelBlue')
        # Labeling the markers with Values and Gage
        xy_temp = []
        for i in range(len(y_)):
            if i == 0:
                xy = [x_[0].astype('str'), y_[0]]
                xy_temp.append(xy)
            else:
                xy = [x_[i - 1].astype('str'), y_[i - 1]]
                xy_temp.append(xy)

            # Creating values in text inside the plot
            ax[pos].text(x=xy[0], y=xy[1], s=str(xy[1]), color='k', fontweight='bold', fontsize=12)

            for val_gage, xy in zip(y_gage, xy_temp):
                annot = ax[pos].annotate(val_gage, xy=xy, xytext=(-20, 20), textcoords="offset points",
                                         bbox=dict(boxstyle="round", fc="w"),
                                         arrowprops=dict(arrowstyle="->"))
                # annot.set_visible(False)

    # Function for storing and showing the clicked values
    def update_annot(ind):
        print("Enter update_annot")
        coord = steps[0].get_offsets()[ind["ind"][0]]
        annot.xy = coord
        text = "{}, {}".format(" ".join(list(map(str, ind["ind"]))),
                                " ".join([y_gage[n] for n in ind["ind"]]))
        annot.set_text(text)
        annot.get_bbox_patch().set_alpha(0.4)

    def on_click(event):
        print("Enter on_click")
        vis = annot.get_visible()
        # print(event.inaxes)
        # print(ax[pos])
        # print(event.inaxes == ax[pos])
        if event.inaxes == ax[pos]:
            cont, ind = steps[0].contains(event)
            if cont:
                update_annot(ind)
                annot.set_visible(True)
                fig.canvas.draw_idle()
            else:
                if vis:
                    annot.set_visible(False)
                    fig.canvas.draw_idle()

    fig.canvas.mpl_connect("button_press_event",on_click)

    plt.show()

plot_signals(signals_df)

I've tested and reviewed many answers and code like the following:

I even reviewed the mplcursors module for a long time, since it comes with an example with a graph of steps similar to what I'm doing: https://mplcursors.readthedocs.io/en/stable/examples/step.html, but it gives me the same result and I can't find the solution.



from How to make labels appear when clicking on a step plot subplot point in matplotlib?

Showing class attributes in the PyCharm debugger when subclassing str

I want to see class attributes for a subclass of str in the debugger.

Example image

Refer to the picture - I want to have the dropdown arrow next to a (which is a POSString object). In other words, in the Pycharm debugger I want to have the option to doubleclick a and see the attribute bar with value "HELLO", exactly like for the foo object.

Is this possible in PyCharm, and if yes how?

The two classes:

class Foo:
    bar = "HELLO"


class POSString(str):
    bar = "HELLO"

    def __init__(self, pos="", *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.pos = pos

    def __hash__(self):
        return hash((str(self), self.pos))

    def __eq__(self, other):
        return super().__eq__(other) and self.pos == other.pos

And how I create my two objects:

foo = Foo()
a = POSString("banana")

I am using Python 3.7.1.

PyCharm:

PyCharm 2018.2.5 (Professional Edition)
Build #PY-182.5107.22, built on November 13, 2018
JRE: 1.8.0_152-release-1248-b22 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.15.0-43-generic


from Showing class attributes in the PyCharm debugger when subclassing str

Coding Custom Likelihood Pymc3

I am struggling to implement a linear regression in pymc3 with a custom likelihood.

I previously posted this question on CrossValidated & it was recommended to post here as the question is more code orientated (closed post here)

Suppose you have two independent variables x1, x2 and a target variable y, as well as an indicator variable called delta.

  • When delta is 0, the likelihood function is standard least squares
  • When delta is 1, the likelihood function is the least squares contribution only when the target variable is greater than the prediction

enter image description here

Example snippet of observed data:

x_1  x_2  𝛿   observed_target  
10    1   0   100              
20    2   0   50               
5    -1   1   200             
10   -2   1   100             

Does anyone know how this can be implemented in pymc3? As a starting point...

model =  pm.Model()
with model as ttf_model:

  intercept = pm.Normal('param_intercept', mu=0, sd=5)
  beta_0 = pm.Normal('param_x1', mu=0, sd=5)
  beta_1 = pm.Normal('param_x2', mu=0, sd=5)
  std = pm.HalfNormal('param_std', beta = 0.5)

  x_1 = pm.Data('var_x1', df['x1'])
  x_2 = pm.Data('var_x2', df['x2'])

  mu = (intercept + beta_0*x_0 + beta_1*x_1)


from Coding Custom Likelihood Pymc3

Friday, 29 October 2021

Effect cleanup potential issue in React 17 for mutable value inside effect

In React 17, the effect cleanup function always runs asynchronously — for example, if the component is unmounting, the cleanup runs after the screen has been updated.

In the changelog React team has highlighted a potential issue and solution for this:

Problematic Code:

useEffect(() => {
  someRef.current.someSetupMethod();
  return () => {
    someRef.current.someCleanupMethod();
  };
});

Solution suggested by the React team:

The problem is that someRef.current is mutable, so by the time the cleanup function runs, it may have been set to null. The solution is to capture any mutable values inside the effect.

useEffect(() => {
  const instance = someRef.current;
  instance.someSetupMethod();
  return () => {
    instance.someCleanupMethod();
  };
});

While the above solution works in most cases, I have created a custom hook, where this solution is problematic:

function useMountEffect(funcForMount, funcForUnmount) {
  const funcRef = useRef();
  funcRef.current = { funcForMount, funcForUnmount };
  useEffect(() => {
    funcRef?.current?.funcForMount?.();
    return () => funcRef?.current?.funcForUnmount?.();
  }, []);
}

In the above example, I explicitly don't want to capture the mutable value, otherwise, I'll have to re-run the effect when funcForUnmount changes.
The reason to create the hook in this way is to have the freedom to call the function only on mount / unmount without worrying about stale closure and dependency array.

Update: I have found one approach for useMountEffect

function useMountEffect(funcForMount, funcForUnmount) {
  const isMounted = useRef();
  useEffect(() => {
    return () => {
      isMounted.current = false;
    };
  }, []);
  useEffect(() => {
    if (!isMounted.current) {
      isMounted.current = true;
      funcForMount?.();
    }
    return () => !isMounted.current && funcForUnmount?.();
  });
}

But the major questions here are for 'The problem is that someRef.current is mutable, so by the time the cleanup function runs, it may have been set to null.':

  • How can the ref.current go null between unmount and cleanup call in React 17?
  • Who is setting the value null? Is it react which can set ref.current to null? Or, is it the user who can change in between?
  • If a user can change in between how is this possible? Why was it not an issue prior to React 17?


from Effect cleanup potential issue in React 17 for mutable value inside effect

How to get sympy to give solutions with smaller coefficients

Consider the following example Diophantine equation

-118w + 989x + 918y  -512z == 0

You can solve this in sympy with:

import sympy
from sympy.solvers.diophantine import diophantine 
w, x, y, z = sympy.symbols('w x y z')
a, b, c, d = -118, -989, 918, -512
diof = list(diophantine(a*w -b*x +c*y -d*z ))[0]
print(diof)

which gives:

(t_0, -118*t_0 + 2*t_1, 1690468*t_0 - 28425*t_1 + 256*t_2, -3031184*t_0 + 50969*t_1 - 459*t_2)

Explanation: This solution allows you to pick three integers t_0, t_1, t_2 freely and from there you can compute the values of w, x, y, z.

This is fine except the coefficients are always much larger than they need to be. If you solve the same system in Mathematica you get:

x = 2 c_2, y = 9 c_2 + 256 c_3 + 81 w, z = 20 c_2 + 459 c_3 + 145 w, c_2 element Z and c_3 element Z

or:

enter image description here

Is it possible to get sympy to give solutions to linear Diophantine equations with similarly small coefficients?



from How to get sympy to give solutions with smaller coefficients

Track changes to multiple Ckeditor 5 instances in Javascript

I'm accessing an iframe thru fancybox 3 with multiple ckeditor. It's working well but I want to track if a user has inputted anything on the textboxes so that I can change the button of the fancybox and give them an alert once they hit close. This is my JS code on the iframe page:

  window.editors = {};

  document.querySelectorAll( '.editor' ).forEach( ( node, index ) => {
    ClassicEditor
      .create( node, {
        removePlugins: ['Heading', 'BlockQuote', 'CKFinderUploadAdapter', 'CKFinder', 'EasyImage', 'Image', 'ImageCaption', 'ImageStyle', 'ImageToolbar', 'ImageUpload', 'MediaEmbed'],
      } )
      .then( newEditor => {
        window.editors[ index ] = newEditor;
      } );

      editors[ index ].document.on( 'change', () => {
        console.log( 'Alert: The document has changed!, are you sure you want to close without saving?' );
      } );

  } );

But it's giving me an error:

ifram.php:1070 Uncaught TypeError: Cannot read properties of undefined (reading 'document')

at ifram.php:1070

at NodeList.forEach (<anonymous>)

at ifram.php:1060

I also tried taking this out off document.querySelectorAll() function

  editors.document.on( 'change', () => {
    console.log( 'Alert: The document has changed!, are you sure you want to close without saving?' );
  } );

But it gave me another error: Uncaught TypeError: Cannot read properties of undefined (reading 'on')



from Track changes to multiple Ckeditor 5 instances in Javascript

NodeJS child_process stdout, if process is waiting for stdin

I'm working on an application, which allows to compile and execute code given over an api.

The binary I want to execute is saved as input_c and should print a text, asking the user for his name and print out another text after the input is received.

It correctly works using the code below: first text - input (on terminal) - second text.

const {spawn} = require('child_process');
let cmd = spawn('input_c', [], {stdio: [process.stdin, process.stdout, process.stderr]});

Output:

$ node test.js
Hello, what is your name? Heinz
Hi Heinz, nice to meet you!

I like to handle stdout, stderr and stdin seperately and not write it to the terminal. The following code was my attempt to achieve the same behaviour as above:

const {spawn} = require('child_process');

let cmd = spawn('input_c');

cmd.stdout.on('data', data => {
    console.log(data.toString());
});

cmd.stderr.on('data', data => {
    console.log(data.toString());
});

cmd.on('error', data => {
    console.log(data.toString());
});

// simulating user input
setTimeout(function() {
    console.log('Heinz');
    cmd.stdin.write('Heinz\n');
}, 3000);

Output:

$ node test.js
Heinz
Hello, what is your name? Hi Heinz, nice to meet you!

To simulate user input I'm writing to stdin after 3000ms. But here I'm not receiving the first data in stdout directly on run, it seems to wait for stdin and outputs everything at once.

How can I achieve the same behaviour for my second case?

The following C-Code was used to compile the binary, but any application waiting for user input can be used for this:

#include <stdio.h>

int main() {
    char name[32];
    printf("Hello, what is your name? ");
    scanf("%s", name);
    printf("Hi %s, nice to meet you!", name);
    return 0;
}


from NodeJS child_process stdout, if process is waiting for stdin

How to achieve Scrollspy with hash routing using react

I am working with react (Hooks), and I have done a lot of things, but here I have been stuck on one place from few days.

I want to create a scrollspy using react that too with routing, where when on scroll new menu active I want to change the route

Please refer this link This is what I am trying to achieve, here they are using Hash routing.

What I am doing

  1. I have one sidebar, where I have some Menus.
  2. I am looping data and showing the sidebar, which is working fine.

What I am trying to achieve

  1. For each menu I have some data, I want to show that
  2. So when I scroll and the first menu text ends I want to active the next menu, but also with change of rout
  3. Like have some routes which are --- /home , /profile , /status , /info.
  4. So when I **scrollspy **changes the menu I want to change the route also as I have data from Api.
  5. I am not able to show the data In the particular rout, like when I click on Profile I want to show that data in that page, but Not bale to pass that data.

Doing it with simple on click of routing is fine and I am able to achieve it easily, but here I want to achieve something else and facing hard time to do it.

JSON data

    [
    {
      menu: "HOME",
      path: "home",
      data:
        "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
    },
    {
      menu: "PROFILE",
      path: "profile",
      data:
        "The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from 'de Finibus Bonorum et Malorum' by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham."
    },
    {
      menu: "STATUS",
      path: "status",
      data:
        "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English."
    },
    {
      menu: "INFO",
      path: "info",
      data:
        "Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). q"
    }
  ]

In the above data I have data which I want to show show in each respective rout, on scroll or on click.

On click of I have able to change rout and rendering the clicked page.

My code

Below is my Menu bar .js file

<div className="menuBar">
  <Nav className="mr-auto ml-1">
    {data.map((li, ind) => (
      <li className="nav-item" key={li.path}>
        <NavLink
          className="nav-link"
          activeClassName="active"
          to={`${li.path}`}
          onClick={() => OnclickSidebar(li.menu, li.path)}
        >
          {li.menu}
        </NavLink>
      </li>
    ))}
  </Nav>
</div>

route.js file

 <Switch>
    <Route exact path="/" component={Home} />
    <Route exact path="/home" component={Home} />
    <Route path="/profile" component={Profile} />
    <Route path="/status" component={Status} />
    <Route path="/info" component={Info} />
    <Route path="/*" component={() => "Page not found"} />
  </Switch>

Above is What I have achieved till now, Now I am not getting any idea to move forward, so seeking for some idea here.

PS : please refer This is what I exactly i want, here hash routing has been used.

Edit / Update

I have data like above, and I have shared the link what i am trying to achieve, I am open to take suggestion on How to I can approach this. As I am totally new on this Part, not getting the idea of how I should start to achieve this.

Sharing more info

When I open the page the initial route is showing Check this image it shows the public route

Now when I scroll on sidebar Overview became active and the overview route shows up Check this image

So above I want to achieve.

similarly it goes for all the sidebar menus, now sidebar has some sub menus also, which works exact at the same way, if I am able to achieve with one flow this sub menu I can do by myself. I just need a head start for this.

My code sandbox link

How to check for sub menu and open when menu is active

Actually my data have some sub menus

    [
    {
        menu: 'HOME',
        path: 'home',
        data:
            "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
    },
    {
        menu: 'PROFILE',
        path: 'profile',
        data:
            "The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from 'de Finibus Bonorum et Malorum' by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.",
        Sub_menu: [ // some menus have submenus also
            {
                menu: 'Profile1',
                path: '/profile/profile1',
                data: 'Some data for the sub menu',
            },
            {
                menu: 'Profile2',
                path: '/profile/profile2',
                data: 'Some data for the sub menu 2',
            },
        ],
    },
    {
        menu: 'STATUS',
        path: 'status',
        data:
            "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.",
    },
    {
        menu: 'INFO',
        path: 'info',
        data:
            "Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). q",
        Sub_menu: [
            {
                menu: 'info1',
                path: '/info/info1',
                data: 'Some data for the sub menu info',
            },
            {
                menu: 'info2',
                path: '/info/info2',
                data: 'Some data for the sub menu info2',
            },
        ],
    },
];

When Menu having sub menu is active

When sub menu is active route changes as usual



from How to achieve Scrollspy with hash routing using react

after upgrade to Webpack 5 not able to access the menifest in copy-webpack-plugin

My config was working fine in webpack version 4.6.0 and webpack-assets-manifest version 3.1.1

since I upgraded to webpack 5 and webpack-assets-manifest to 5. I'm not getting value in my manifestObject it's just empty object

I suspect this is happening because transform function running before manifest is created looked into the new documentation of webpack-assets-manifest but could not get it working

my goal is to access manifest value in transform function, but it looks like transform function is running before manifest is generated

var CopyWebpackPlugin = require('copy-webpack-plugin');
var SaveHashes = require('webpack-assets-manifest');

const manifest = new SaveHashes({
    entrypoints: true,
    entrypointsKey: 'entryPoints'
});
module.exports = {
    entry: {
        main: ['./src/apps/main'],
        games: ['./src/apps/games'],
    },
    output: {
        path: path.join(__dirname, 'dist'),
        publicPath: assetsUrl,
        filename: 'assets/javascript/[name].[contenthash].js',
        chunkFilename: 'assets/javascript/[name].[contenthash].js'
    },
    .
    .
    .
    .
    .
    plugins: [
        new CleanWebpackPlugin(),
        manifest,
        new CopyWebpackPlugin([
            {
                from: './views/**/*',
                to: path.join(__dirname, 'dist'),
                transform(content, path) {
                    // I want to access manifest here
                    // so that I can inject the final script(javascript bundle with content hash)
                    // in my view template
                    const manifestObject = JSON.parse(manifest);
                }
            }
        ])
    ]
};


from after upgrade to Webpack 5 not able to access the menifest in copy-webpack-plugin

performing many gradient-based optimizations in parallel with TensorFlow

I have a model which requires solving a system of ODEs with tfp.math.ode.BDF, and I would like to find the individual least-squares fits of this model to n > 1000 datasets. That is to say, if my model has m parameters then at the end of the optimization process I will have an n by m tensor of best-fit parameter values.

What would be the best way to perform this optimization in parallel? At this point I’m planning to define an objective function that adds up the n individual sums of square residuals, and then uses tfp.optimizer.lbfgs_minimize to find the best-fit values of the combined n×m parameters.



from performing many gradient-based optimizations in parallel with TensorFlow

MediaCodec seeking backwards

Firstly, I'm aware that there are similar questions asked before. But my question is about performance.


I'm using MediaCodec to seek through a video, this works perfectly when seeking forward. But when seeking backward, it is "choppy"/"laggy".

This is 'kind of' expected because, when seeking backwards, I seek to the previous key/sync frame and then seek forward to the desired frame, like this:

extractorSeekTo(timestamp, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);
mDecoder.flush();
//continue feeding without rendering:
//mDecoder.releaseOutputBuffer(bufferIndex, false); 
//until target frame is reached

Extra context:
I'm testing with a video that only has 2 key frames. When seeking backwards, it would jump all the way back to the closest key frame and then seek forward. The time it takes depends on how "close" the desired buffer is to the key frame.


I've seen some applications that perform backward seeking without having the jump/lag effect. I decompiled these applications to see if they use native code and they all do. That got me thinking that MediaCodec is not performing optimally.

So, my question is (the obvious question would be 'how these applications do it', but..):

  • Would implementing MediaCodec using ndk/natively resolve this issue? (improve the performance)
  • If not, does anyone have any suggestions on how the lag can be overcome?


from MediaCodec seeking backwards

How to import a function in one module to a module in a different folder?

Imagine a package with this structure:

└── main_package
    ├── __init__.py
    ├── subpackage1
    │   ├── __init__.py
    │   └── module1.py
    └── subpackage2
        ├── __init__.py
        └── module2.py

What is the best way of importing a function in module1 to module2?

Should the __init__.py files contain something?

The best solution I have found is to include in module2

import sys
sys.path.append('./')
from main_package.subpackage1.module1 import fun1

And when running module2 from the directory in which main_package is it works.

But I feel like this does not take advantage of the fact that they are packages. And also it would be nice to be able to run this from wherever I want if possible.



from How to import a function in one module to a module in a different folder?

Xamarin.Android Not Receiving Location Updates in Direct Boot Mode

TLDR: is it possible to access location in Direct Boot mode?

I want to receive location updates for a device (testing on Pixel 3a) when the phone boots. This is a companion feature for a regular app, so assume required permission grants etc are taken care of.

The following code is also taken from components that work fine when invoked from a MainActivity.

The steps to set up the location callbacks are as follows:

Firstly, a DirectBootAware BroadcastReceiver that listens for ACTION_LOCKED_BOOT_COMPLETED means I can run code when the device boot and before the screen is unlocked.

[BroadcastReceiver(Enabled = true, Exported = true, DirectBootAware = true)]
[IntentFilter(new string[] { Intent.ActionLockedBootCompleted })]
public class StartForegroundServiceReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {

From here I can verify that the permissions for Corse and Fine location are granted (which they are!)

var permissionCheckResult = context.CheckSelfPermission(Manifest.Permission.AccessFineLocation);

Android.Util.Log.Info("TestingTag", $"AccessFineLocation CheckSelfPermission {permissionCheckResult.ToString()}");


permissionCheckResult = context.CheckSelfPermission(Manifest.Permission.LocationHardware);

Android.Util.Log.Info("TestingTag", $"LocationHardware CheckSelfPermission {permissionCheckResult.ToString()}");


permissionCheckResult = context.CheckSelfPermission(Manifest.Permission.AccessCoarseLocation);

Android.Util.Log.Info("TestingTag", $"AccessCoarseLocation CheckSelfPermission {permissionCheckResult.ToString()}");

and that the Network, GPS, and Passive Location Providers are enabled (again, they're all enabled!)

var locationManager = (LocationManager)context.GetSystemService(Context.LocationService);

var enabled = locationManager.IsProviderEnabled(LocationManager.PassiveProvider);
Log.Info("TestingTag", $"PassiveProvider IsProviderEnabled {(enabled ? "yes" : "no")}");

enabled = locationManager.IsProviderEnabled(LocationManager.GpsProvider);
Log.Info("TestingTag", $"GpsProvider IsProviderEnabled {(enabled ? "yes" : "no")}");

enabled = locationManager.IsProviderEnabled(LocationManager.NetworkProvider);
Log.Info("TestingTag", $"NetworkProvider IsProviderEnabled {(enabled ? "yes" : "no")}");

If I check GetLastKnownLocation

var location = locationManager.GetLastKnownLocation(LocationManager.GpsProvider);

if (location == null)
{
    location = locationManager.GetLastKnownLocation(LocationManager.NetworkProvider);
}

if (location == null)
{
    location = locationManager.GetLastKnownLocation(LocationManager.PassiveProvider);
}

if (location == null)
    Log.Error("TestingTag", $"Location is null");

This always returns null, perhaps because the device has just booted? Or maybe it's because of the same root cause as not receiving any location updates?

Finally, I register for location updates with the LocationManager

var callback = new LocationCallback();

Log.Info("TestingTag", "Starting RequestLocationUpdates via PassiveProvider..");
locationManager.RequestLocationUpdates(LocationManager.PassiveProvider, 3000, 3, callback);

Log.Info("TestingTag", "Starting RequestLocationUpdates via GpsProvider..");
locationManager.RequestLocationUpdates(LocationManager.GpsProvider, 3000, 3, callback);

Log.Info("TestingTag", "Starting RequestLocationUpdates via NetworkProvider..");
locationManager.RequestLocationUpdates(LocationManager.NetworkProvider, 3000, 3, callback);

Here's the location callback code:

public class LocationCallback : ScanCallback, Android.Locations.ILocationListener
{
    public void OnLocationChanged(Location location)
    {
        Log.Info("TestingTag", $"OnLocationChanged {location.Latitude} ~ {location.Longitude}");
    }

    public void OnProviderDisabled(string provider)
    {
        Log.Info("TestingTag", $"OnProviderDisabled {provider}");
    }

    public void OnProviderEnabled(string provider)
    {
        Log.Info("TestingTag", $"OnProviderEnabled {provider}");
    }

    public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras)
    {
        Log.Info("TestingTag", $"OnStatusChanged {provider} ~ {status.ToString()}");
    }
}

The callback is never invoked :-(

I've added a BluetoothManager to scan for BLE signals (omitted code for that) and that works, ScanResults are returned fine and the phone / BroadcastReceiver is still alive. So it's not as if the BroadcastReceiver is just dying...

Any tips / suggestions / or confirmation that what I'm trying to do is not possible would be greatly appreciated!

Thanks!



from Xamarin.Android Not Receiving Location Updates in Direct Boot Mode

Is there a way to use Dart Sass command line version with web pack?

From the dart-sass documentation we see that the dart-sass has a command-line version and is more performant.

I want to know if we can somehow use that command-line version with the existing webpack set up to speed up our build-time performance.

I can install dart libraries in the build machine. But is there any webpack plugin which can leverage that and use the machine dependencies to build sass rather than dart-sass npm?



from Is there a way to use Dart Sass command line version with web pack?

How to run an integration test for the Repository layer of an Android app with Firestore as a backend using Flow

I am currently trying to write an integration test for my repository layer that tests if I call a method, getExercises(), then it returns List<Exercise>, provided that the data is loaded into the local Firestore emulator ahead of time.

So far I got the local Firestore emulator to switch on and off at the beginning/end of a test run, respectively. I am able to populate my data into Firestore, and see the data in the local Firestore emulator via the web UI.

My problem is that my test assertion times out because the Task (an asynchronous construct the Firestore library uses), blocks the thread at the await() part in the repository method.

Test

package com.example.fitness.data

import androidx.test.ext.junit.runners.AndroidJUnit4
import app.cash.turbine.test
import com.example.fitness.Constants.EXERCISES_REF
import com.example.fitness.FirebaseEmulatorTest
import com.google.android.gms.tasks.Tasks
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import javax.inject.Inject
import kotlin.time.Duration
import kotlin.time.ExperimentalTime

@HiltAndroidTest
@RunWith(AndroidJUnit4::class)
class ExerciseRepositoryTest : FirebaseEmulatorTest() {
    @get:Rule
    var hiltRule = HiltAndroidRule(this)

    @Inject
    lateinit var subject: ExerciseRepository

    @Before
    fun setup() {
        hiltRule.inject()
    }

    @ExperimentalTime
    @Test
    fun `#getExercises returns a flow of exercises`() = runBlocking {
        val exercises = mutableListOf<Exercise>().apply {
            add(Exercise("a", "pushups"))
            add(Exercise("b", "pull-ups"))
            add(Exercise("c", "sit-ups"))
        }

        runBlocking(Dispatchers.IO) {
            val task1 = firestoreInstance.collection(EXERCISES_REF).add(exercises.first())
            val task2 = firestoreInstance.collection(EXERCISES_REF).add(exercises[1])
            val task3 = firestoreInstance.collection(EXERCISES_REF).add(exercises.last())

            Tasks.await(task1)
            Tasks.await(task2)
            Tasks.await(task3)

            println("Done with tasks: task1: ${task1.isComplete}. task2: ${task2.isComplete}. task3: ${task3.isComplete}.")
        }

        println("About to get exercises")

        subject.getExercises().test(timeout = Duration.seconds(5)) {
            println("test body")

            assertThat(awaitItem().size, `is`(4)) // Just checking that it passes for the right reasons first. This number should be 3
        }
    }
}

Repository (System under test)

package com.example.fitness.data

import com.example.fitness.Constants.EXERCISES_REF
import com.google.firebase.firestore.CollectionReference
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.tasks.await
import javax.inject.Inject
import javax.inject.Named
import javax.inject.Singleton

@Singleton
class ExerciseRepository @Inject constructor(
    @Named(EXERCISES_REF) private val exerciseCollRef: CollectionReference
) {
    fun getExercises() = flow<List<Exercise>> {
        println("beginning of searchForExercise")

        val exercises = exerciseCollRef.limit(5).get().await() // NEVER FINISHES!!
        println("Exercise count: ${exercises.documents}")

        emit(exercises.toObjects(Exercise::class.java))
    }
}

The output of this results in:

Done with tasks: task1: true. task2: true. task3: true.
About to search for exercises
beginning of searchForExercise
test body

Timed out waiting for 5000 ms
kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 5000 ms

"Exercise count: 3" message never prints!

Note: I am using Robolectric 4.6.1, kotlinx-coroutines-playservices (1.5.0) to provide the await() extension function, and the Turbine testing library for flow assertions (0.6.1)

Perhaps of relevance is a superclass this test inherits that sets the main dispatcher to a test dispatcher.

package com.example.fitness

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.*
import org.junit.After
import org.junit.Before
import org.junit.Rule

abstract class CoroutineTest {
    @Rule
    @JvmField
    val rule = InstantTaskExecutorRule()

    protected val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()
    private val testCoroutineScope = TestCoroutineScope(testDispatcher)

    @Before
    fun setupViewModelScope() {
        Dispatchers.setMain(testDispatcher)
    }

    @After
    fun cleanupViewModelScope() {
        Dispatchers.resetMain()
    }

    @After
    fun cleanupCoroutines() {
        testDispatcher.cleanupTestCoroutines()
        testDispatcher.resumeDispatcher()
    }

    fun runBlockingTest(block: suspend TestCoroutineScope.() -> Unit) =
        testCoroutineScope.runBlockingTest(block)
}

Any help here would be greatly appreciate.

Edit I have opened an issue with the kotlin extensions team to get more visibility on how to go about testing this, including a repo demonstrating the problem.



from How to run an integration test for the Repository layer of an Android app with Firestore as a backend using Flow

How to make Images/PDF of Timetable using Python

I am solving a timetable scheduling problem and wants to print out the Final Output in the form of PDF or set of images. I have multiple sections and each section has its own schedule.

I have created a 2D array for each section. The array is of 5 x 5 size (5 days, each day has 5 five slots) and each index of the array represents a lecture slot. Now, this 2D array contains the lectures for every course in the timetable of that specific section. Sample Output is below, (It's a dictionary, and each key is a section. Values against each key is a 2D array

CS-3B :  [['', '', 'DS ', '', 'COaAL '], ['', 'COaAL ', '', 'DS ', 'OOP '], ['DS-L ', 'DS-L ', 'OOP-L ', 'OOP-L ', 'FoM '], ['COaAL-L ', 'COaAL-L ', 'OOP ', '', ''], ['', 'FoM ', 'DE ', '', 'DE ']]
SE-3A :  [['', 'OOP-L ', 'OOP-L ', '', 'SRE '], ['SRE ', 'OOP ', 'DS-L ', 'DS-L ', ''], ['', 'DS ', '', '', 'MM '], ['DS ', 'MM ', '', 'LA ', ''], ['OOP ', 'HCI ', '', 'LA ', 'HCI ']]
CS-7F :  [['', '', '', '', ''], ['RSaG ', '', '', '', ''], ['ST ', '', 'RSaG ', '', ''], ['', '', '', '', ''], ['', 'ST ', '', '', '']]
CS-1C :  [['IS ', 'ECaC-L ', 'ECaC-L ', '', 'PF '], ['ECaC ', 'PF-L ', 'PF-L ', 'ECaC-L ', 'ECaC-L '], ['DLD ', 'ECaC ', '', 'PF ', 'ItIaCT '], ['DLD-L ', 'DLD-L ', 'IS ', 'LA ', ''], ['ECaC ', 'ECaC ', 'ItIaCT ', 'DLD ', 'LA ']]
CS-1D :  [['PF-L ', 'PF-L ', 'ECaC-L ', 'ItIaCT ECaC-L ', 'ItIaCT '], ['IS ', 'AP ', 'ECaC-L ', 'ECaC-L ', ''], ['PF ', 'PF ', '', 'ECaC ', ''], ['CaAG ', 'ECaC ', 'ECaC ', '', 'IS '], ['', 'CaAG ', '', 'ECaC ', 'AP ']]
CS-7A :  [['', 'DM ', '', 'PPiI ', 'DS '], ['AI-L ', 'AI-L ', '', 'AI ', 'IS '], ['', '', 'DS ', '', ''], ['SE ', 'SE ', '', 'PPiI ', ''], ['', 'AI ', 'IS ', '', 'DM ']]
CS-7B :  [['', 'DS ', '', 'DS ', 'DM '], ['', '', '', 'PPiI ', ''], ['', 'PPiI ', '', 'SE ', ''], ['', 'DM ', '', 'IS ', ''], ['', '', 'IS ', 'SE ', '']]
CS-1B :  [['LA ', '', '', 'DLD ', 'DLD '], ['ECaC ', 'IS ', '', 'PF ', 'ECaC '], ['ECaC-L ', 'ECaC-L ', 'DLD-L ', 'DLD-L ', 'ItIaCT '], ['ECaC ', 'PF-L ', 'PF-L ', 'ECaC-L ', 'ECaC-L '], ['ECaC ', 'PF ', 'IS ', 'LA ', 'ItIaCT ']]
CS-1A :  [['', 'PF-L ', 'PF-L ', 'ECaC ', ''], ['ECaC ', '', 'ItIaCT ', 'LA ', 'ECaC '], ['PF ECaC-L ', 'ItIaCT ECaC-L ', '', 'DLD-L ', 'DLD-L '], ['IS ', 'PF ', 'ECaC-L ', 'ECaC-L ', ''], ['DLD ', 'IS ', 'LA ', 'DLD ', 'ECaC ']]
CS-7D :  [['AML ', '', 'IS ', '', 'AML '], ['', '', '', '', ''], ['IS ', 'SfMD ', '', '', ''], ['', '', '', '', 'SfMD '], ['PPiI ', '', 'PPiI ', '', '']]
CS-7C :  [['SfMD ', '', '', 'AML ', ''], ['PPiI ', '', '', '', ''], ['', 'SfMD ', '', '', ''], ['', '', 'AML ', 'IS ', ''], ['', '', 'PPiI ', 'IS ', '']]
CS-3C :  [['MM ', 'COaAL-L ', 'COaAL-L ', 'DS ', ''], ['', '', '', '', ''], ['DS-L ', 'DS-L ', 'DS ', '', 'DE '], ['', '', '', '', ''], ['', 'DE ', '', '', 'MM ']]
CS-5C :  [['', 'CN-L ', 'CN-L ', '', 'CN '], ['PaS ', 'CN ', '', '', 'ToA '], ['', '', '', 'SDaA ', 'AP '], ['AP ', '', '', 'ToA ', 'SDaA '], ['', 'PaS ', '', '', '']]
CS-5B :  [['', '', 'WP ', '', ''], ['WP ', 'ToA ', 'MM ', 'CN-L ', 'CN-L '], ['SDaA ', '', '', 'MM ', 'CN '], ['SDaA ', '', '', 'ToA ', ''], ['', '', '', 'CN ', '']]
CS-1E :  [['PF-L ', 'PF-L ', 'AP ', 'ECaC ', 'ECaC '], ['ECaC-L ', 'ECaC-L ', 'PS ', 'ItIaCT ', 'AP '], ['', 'PF ', 'CaAG ', 'ECaC-L ', 'ECaC-L '], ['PS ', '', 'ItIaCT ', '', ''], ['', 'CaAG ', 'PF ', 'ECaC ', 'ECaC ']]
SE-3B :  [['LA ', '', '', '', ''], ['DS ', 'HCI ', '', '', ''], ['DS ', 'LA ', '', '', ''], ['', 'DS-L ', 'DS-L ', 'SRE ', 'F&A '], ['F&A ', 'HCI ', '', '', 'SRE ']]
SE-5B :  [['', '', '', 'PaS ', 'TaBW '], ['SCaD-L ', 'SCaD-L ', 'SCaD ', 'OR ', 'SQE '], ['', '', 'TaBW ', '', 'SCaD '], ['', 'SQE ', '', '', ''], ['PaS ', '', '', '', 'OR ']]
SE-5A :  [['OS-L ', 'OS-L ', 'OS ', 'SCaD-L ', 'SCaD-L '], ['OR ', 'DS ', '', 'OR ', 'TaBW '], ['DS-L ', 'DS-L ', 'PaS ', 'SCaD ', 'OS '], ['', 'SQE ', 'SCaD ', 'PaS ', 'TaBW '], ['', '', 'DS ', '', 'SQE ']]
CS-3A :  [['DS-L ', 'DS-L ', 'LA ', 'CaAG ', 'DS '], ['F&A ', 'DS ', 'DLD ', 'DS ', 'OOP '], ['CaAG ', 'LA ', 'COaAL ', 'OOP-L ', 'OOP-L '], ['DE AP ', 'COaAL-L ', 'COaAL-L ', 'OOP ', 'COaAL '], ['AP ', 'DE ', 'F&A ', 'DLD ', 'DS ']]

Please note CS-1D as example in this,

CS-1D :  [['PF-L ', 'PF-L ', 'ECaC-L ', 'ItIaCT ECaC-L ', 'ItIaCT '], ['IS ', 'AP ', 'ECaC-L ', 'ECaC-L ', ''], ['PF ', 'PF ', '', 'ECaC ', ''], ['CaAG ', 'ECaC ', 'ECaC ', '', 'IS '], ['', 'CaAG ', '', 'ECaC ', 'AP ']]

There are two things that I need to take care of. First, every Lab (courses ending with -L have lectures in consecutive slots. So that means, I want the cells (two cells in timetable) to be horizontally merged when represneting a Lab.

Second, at some indexes, there are two lectures happening at the same time. For example, notice the 4th slot of Monday (0 index) in CS-1D. ItIaCT and ECaC-L are two different courses but have lectures at the same time. (In this 2D Array, if there are two or more lectures happening at the same time, then they are separated by a space in that index). For this, I want the cell of that lecture slot to be horizontally divided to fit in both the lectures.

A sample final output looks something like this (each cell will also tell what instructor is teaching the course and in which room the class is being held)

I do not want 13 different slots, but instead only five slots per day. My problem is,

  • I have to do this using Python and I do not know how to start. I have timetables created using an algorithm for each section (as shown above) but I can't figure out how to make a timetable (Output) out of this

  • Secondly, I want to make a PDF File that will contain Timetable of all the Sections. I don't know how to do it. I am assuming that I need to make a Image for each section's timetable and then combine all those images (just like I shared one image of one section's timetable above) into a PDF. However, I do not know how would I convert one timetable to Image.

Also, please note that I kind of made something similar by using plain HTML, the code and result of which I will share below. I am trying to replicate a bit something similar using Python.

<!DOCTYPE html>
<html>
  <style>
.center
{
  text-align: center;
 
}
td{
  height:75px;
  width:150px;
}


  </style>
<body>
<!-- Heading -->
    <h1 class="center">BCS-7D</h1>

<!-- Table -->
    <table border="5" cellspacing="5" align="center">
        
<!-- Day/Periods -->
        <tr>
            <td class="center" ><br>
                <b>Day/Period</b></br>
            </td>
            <td class="center" >
                <b>I</b>
            </td>
            <td class="center" >
                <b>II</b>
            </td>
            <td class="center">
                <b>III</b>
            </td>
            <td class="center">
                <b>1:15-1:45</b>
            </td>
            <td class="center" >
                <b>IV</b>
            </td>
            <td class="center" >
                <b>V</b>
            </td>
           
        </tr>
<!-- Monday -->
        <tr>
            <td class="center">
                <b>Monday</b></td>
            <td class="center">Linear Algebra, Mr. Raheel Ahmad, Room 1</td>
            <td class="center">X</td>
            <td class="center">X</td>
            <td rowspan="6" class="center">
                <h2>L<br>U<br>N<br>C<br>H</h2>
            </td>
            <td colspan="2" class="center">LAB</td>
        
        </tr>
<!-- Tuesday -->
        <tr>
            <td class="center">
                <b>Tuesday</b>
            </td>
            <td class="center">X</td>
            <td colspan="2" class="center">LAB
            </td>
            
            <td class="center">X</td>
            <td class="center">X</td>
        </tr>
<!-- Wednesday -->
        <tr>
            <td class="center">
                <b>Wednesday</b>
            </td>
            <td class="center">Object Oriented Programming, Ms. Jen Ledger, Room 13<hr>Programming Fundamentals, Mr. Zahid Iqbal, Room 6</td>
            <td class="center">X</td>
            <td class="center">X</td>
            <td class="center">X</td>
            <td colspan="3" class="center">X
            </td>
        </tr>
<!-- Thursday -->
        <tr>
            <td class="center">
                <b>Thursday</b>
            </td>
            <td class="center">X</td>
            <td class="center">X</td>
            <td class="center">X</td>
            <td colspan="3" class="center">Object Oriented Programming - Lab, Ms. Zain Malik, Lab 6
            </td>
          
        </tr>
<!-- Friday -->
        <tr>
            <td class="center">
                <b>Friday</b>
            </td>
            <td colspan="2" class="center">LAB
            </td>
            <td class="center">X</td>
            <td class="center">X</td>
            <td class="center">X</td> 

        </tr>
       
    </table>
</body>
  
</html>

Screenshot of the Output, (Please note that this is hard coded layout. The labs can be anywhere in the timetable (for a lab, two consecutive slots must be combined) and two lectures being at the same time can also happen at any time. For that, there should be a horizontal separator on that lecture slot)



from How to make Images/PDF of Timetable using Python